<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ProjectCollaborator extends Model
{
    use HasFactory;

    protected $fillable = [
        'project_id',
        'user_id',
        'role',
        'can_edit',
        'can_upload_versions',
        'can_manage_milestones',
        'added_at',
    ];

    protected $casts = [
        'can_edit' => 'boolean',
        'can_upload_versions' => 'boolean',
        'can_manage_milestones' => 'boolean',
        'added_at' => 'datetime',
    ];

    /**
     * Get the project this collaborator belongs to
     */
    public function project(): BelongsTo
    {
        return $this->belongsTo(Project::class);
    }

    /**
     * Get the user who is the collaborator
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get permissions as array
     */
    public function getPermissionsAttribute(): array
    {
        return [
            'can_edit' => $this->can_edit,
            'can_upload_versions' => $this->can_upload_versions,
            'can_manage_milestones' => $this->can_manage_milestones,
        ];
    }
}
