<?php

namespace App\Models;

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

class Task extends Model
{
    public const STATUSES = ['pending', 'in_progress', 'completed', 'cancelled'];
    public const PRIORITIES = ['low', 'normal', 'high', 'urgent'];
    public const CATEGORIES = ['general', 'collection', 'approval', 'appointment', 'document', 'finance', 'other'];

    protected $fillable = [
        'branch_id','title','description','category','priority','status','due_at','assigned_to','created_by','updated_by',
        'completed_by','completed_at','cancelled_by','cancelled_at','source_type','source_id','automation_key',
    ];

    protected function casts(): array
    {
        return ['due_at'=>'datetime','completed_at'=>'datetime','cancelled_at'=>'datetime'];
    }

    public function branch(): BelongsTo { return $this->belongsTo(Branch::class); }
    public function assignee(): BelongsTo { return $this->belongsTo(User::class, 'assigned_to'); }
    public function creator(): BelongsTo { return $this->belongsTo(User::class, 'created_by'); }
    public function updater(): BelongsTo { return $this->belongsTo(User::class, 'updated_by'); }
    public function completedBy(): BelongsTo { return $this->belongsTo(User::class, 'completed_by'); }
    public function cancelledBy(): BelongsTo { return $this->belongsTo(User::class, 'cancelled_by'); }
}