<?php

namespace App\Models;

use App\Models\Concerns\BelongsToBusiness;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Project extends Model
{
    use BelongsToBusiness, SoftDeletes;
    protected $guarded = ['id'];
    protected $casts = ['start_date' => 'date', 'due_date' => 'date', 'actual_end_date' => 'date', 'budget' => 'decimal:2', 'contract_value' => 'decimal:2'];
    public function account() { return $this->belongsTo(Account::class); }
    public function deal() { return $this->belongsTo(Deal::class); }
    public function owner() { return $this->belongsTo(User::class, 'owner_id'); }
    public function milestones() { return $this->hasMany(ProjectMilestone::class); }
    public function tasks() { return $this->hasMany(ProjectTask::class); }
    public function products() { return $this->belongsToMany(Product::class)->withPivot(['quantity','notes'])->withTimestamps(); }
    public function activities() { return $this->hasMany(Activity::class); }
    public function meetings() { return $this->hasMany(Activity::class)->where('activity_type_id', 2); }
    public function files() { return $this->morphMany(CrmFile::class, 'fileable'); }
    public function assignments() { return $this->morphMany(CrmAssignment::class, 'assignable'); }
    public function assignees() { return $this->morphToMany(User::class, 'assignable', 'crm_assignments')->withTimestamps(); }
    public function statusHistory() { return $this->morphMany(CrmStatusHistory::class, 'statusable')->latest('changed_at'); }
}
