<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;

class InvestmentProposal extends Model implements Auditable
{
    use HasFactory, SoftDeletes;
    use \OwenIt\Auditing\Auditable;

    protected $fillable = [
        'code', 'name', 'sector', 'description', 'status',
        'investment_value', 'jobs_to_create', 'start_date', 'duration_months',
        'own_capital_pct', 'bank_credit_pct', 'external_investor_pct',
        'province_id', 'district_id', 'address', 'is_special_economic_zone',
        'user_id', 'opportunity_id', 'business_plan_url', 'financial_projections_url',
        'additional_documents', 'progress', 'stages', 'institutions_involved',
        'rejection_reason', 'admin_notes', 'reviewed_by',
        'submitted_at', 'reviewed_at', 'approved_at',
    ];

    protected function casts(): array
    {
        return [
            'additional_documents' => 'array',
            'stages' => 'array',
            'institutions_involved' => 'array',
            'is_special_economic_zone' => 'boolean',
            'start_date' => 'date',
            'submitted_at' => 'datetime',
            'reviewed_at' => 'datetime',
            'approved_at' => 'datetime',
            'investment_value' => 'decimal:2',
        ];
    }

    public function user(): BelongsTo { return $this->belongsTo(User::class); }
    public function province(): BelongsTo { return $this->belongsTo(Province::class); }
    public function district(): BelongsTo { return $this->belongsTo(District::class); }
    public function opportunity(): BelongsTo { return $this->belongsTo(InvestmentOpportunity::class); }
    public function reviewer(): BelongsTo { return $this->belongsTo(User::class, 'reviewed_by'); }
    public function meetings(): HasMany { return $this->hasMany(Meeting::class, 'proposal_id'); }
}
