<?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 Quote extends Model
{
    use HasFactory;

    protected $fillable = [
        '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');

        $scpRunChargeTotal   = $this->screenPrintEntries()->sum('run_charge');
        $scpOtherChargeTotal = $this->screenPrintEntries()->sum('other_charges');
        $scpFleeceTotal      = $this->screenPrintEntries()->sum('fleece');
        $scpFlashTotal       = $this->screenPrintEntries()->sum('flash');
        $scpPolyInkTotal     = $this->screenPrintEntries()->sum('poly_ink');

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

        return $embDigitizingTotal + $embRunChargeTotal + $scpRunChargeTotal + $scpOtherChargeTotal + $scpFleeceTotal + $scpFlashTotal + $scpPolyInkTotal + $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);
    }
}
