<?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;

class Estimate extends Model
{
    use HasFactory;

    protected $fillable = [
        'job_card_id',
        'diagnosis_id',
        'estimate_number',
        'prepared_by_id',
        'labor_cost',
        'parts_cost',
        'miscellaneous_cost',
        'subtotal',
        'tax_rate',
        'tax_amount',
        'discount_amount',
        'total_amount',
        'validity_period_days',
        'terms_and_conditions',
        'status',
        'customer_approval_status',
        'customer_approved_at',
        'customer_approval_method',
        'sent_to_customer_at',
        'sms_sent_at',
        'email_sent_at',
        'notes',
        'internal_notes',
        'revision_number',
        'original_estimate_id',
    ];

    protected $casts = [
        'labor_cost' => 'decimal:2',
        'parts_cost' => 'decimal:2',
        'miscellaneous_cost' => 'decimal:2',
        'subtotal' => 'decimal:2',
        'tax_rate' => 'decimal:4',
        'tax_amount' => 'decimal:2',
        'discount_amount' => 'decimal:2',
        'total_amount' => 'decimal:2',
        'customer_approved_at' => 'datetime',
        'sent_to_customer_at' => 'datetime',
        'sms_sent_at' => 'datetime',
        'email_sent_at' => 'datetime',
    ];

    protected static function boot()
    {
        parent::boot();
        
        static::creating(function ($estimate) {
            if (empty($estimate->estimate_number)) {
                $estimate->estimate_number = 'EST-' . date('Y') . '-' . str_pad(
                    static::whereYear('created_at', now()->year)->count() + 1, 
                    6, 
                    '0', 
                    STR_PAD_LEFT
                );
            }
        });
    }

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

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

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

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

    public function originalEstimate(): BelongsTo
    {
        return $this->belongsTo(Estimate::class, 'original_estimate_id');
    }

    public function revisions(): HasMany
    {
        return $this->hasMany(Estimate::class, 'original_estimate_id');
    }

    public function calculateTotals(): void
    {
        $this->labor_cost = $this->lineItems()->where('type', 'labor')->sum('total_amount');
        $this->parts_cost = $this->lineItems()->where('type', 'parts')->sum('total_amount');
        $this->miscellaneous_cost = $this->lineItems()->where('type', 'miscellaneous')->sum('total_amount');
        
        $this->subtotal = $this->labor_cost + $this->parts_cost + $this->miscellaneous_cost - $this->discount_amount;
        $this->tax_amount = $this->subtotal * ($this->tax_rate / 100);
        $this->total_amount = $this->subtotal + $this->tax_amount;
        
        $this->save();
    }
}
