<?php

namespace App\Models;

use App\Enums\PriceType;
use App\Models\Concerns\InteractsWithActivityLog;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

#[Guarded(['id'])]
#[Appends(['cost_per_unit_formatted', 'price_formatted', 'type_label'])]
class CuttingResultPrice extends Model
{
    // 1. Use Trait
    use HasFactory, InteractsWithActivityLog;

    // 2. Casting
    protected function casts(): array
    {
        return [
            'cost_per_unit' => 'integer',
            'price' => 'integer',
            'price_type' => PriceType::class,
        ];
    }

    // 3. Attribute
    public function costPerUnitFormatted(): Attribute
    {
        return Attribute::make(
            get: fn () => 'Rp '.number_format($this->cost_per_unit, 0, ',', '.'),
        );
    }

    public function priceFormatted(): Attribute
    {
        return Attribute::make(
            get: fn () => 'Rp '.number_format($this->price, 0, ',', '.'),
        );
    }

    public function typeLabel(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->price_type->label(),
        );
    }

    // 4. Relation
    public function cutting(): BelongsTo
    {
        return $this->belongsTo(Cutting::class);
    }

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