<?php

namespace App\Models;

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

class ContractorExperience extends Model
{
    protected $table = 'contractor_experiences';

    protected $fillable = [
        'contractor_id',
        'project_name',
        'client_name',
        'description',
        'value',
        'start_date',
        'end_date',
        'location',
        'certificate_url',
    ];

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

    public function contractor(): BelongsTo
    {
        return $this->belongsTo(Contractor::class);
    }

    public function getDurationInMonths(): int
    {
        if (!$this->start_date || !$this->end_date) {
            return 0;
        }
        return $this->start_date->diffInMonths($this->end_date);
    }

    public function isOngoing(): bool
    {
        return $this->end_date === null || $this->end_date->isFuture();
    }

    public function isCompleted(): bool
    {
        return $this->end_date !== null && $this->end_date->isPast();
    }
}