<?php

namespace App\Models;

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

class Program extends Model
{
    protected $fillable = [
        'program_name',
        'program_description',
        'location',
        'start_date',
        'end_date',
        'pic_user_id',
        'total_budget',
        'status',
        'created_by',
    ];

    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
        'total_budget' => 'decimal:2',
    ];

    public function pic(): BelongsTo
    {
        return $this->belongsTo(User::class, 'pic_user_id');
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function tasks(): HasMany
    {
        return $this->hasMany(Task::class);
    }

    public function targets(): HasMany
    {
        return $this->hasMany(ProgramTarget::class);
    }

    public function taskTargets(): HasManyThrough
    {
        return $this->hasManyThrough(TaskTarget::class, Task::class);
    }

    public function attachments(): HasMany
    {
        return $this->hasMany(Attachment::class, 'program_id');
    }
}
