<?php

namespace App\Models;

use App\Observers\QuoteObserver;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

#[ObservedBy(QuoteObserver::class)]

class Quote extends Model
{
    use HasFactory;

    protected $fillable = [
        'internal_id',
        'customer_id',
        'date',
        'notes',
    ];

    protected $casts = [
        'date' => 'date',
    ];

    protected $appends = [
        'total',
    ];

    public function getTotalAttribute(): float
    {
        $embDigitizingTotal = $this->embroideryEntries()->sum('digitizing_cost');
        $embRunChargeTotal  = $this->embroideryEntries()->sum('run_charge');

        $scpTotal = $this->screenPrintEntries->sum(fn (ScreenPrintEntry $record) => $record->total_price);

        $heatTransferTotal = $this->heatTransferEntries()->sum('price');

        return $embDigitizingTotal + $embRunChargeTotal + $scpTotal + $heatTransferTotal;
    }

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

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

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

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