<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class StockingRecord extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'pond_id', 'cycle_id', 'stocking_date', 'species', 'fish_local_name', 'strain_variety',
        'quantity', 'avg_weight_g', 'supplier_name', 'supplier_contact',
        'cost_per_fingerling', 'transport_cost', 'notes', 'logged_by',
    ];

    protected function casts(): array
    {
        return [
            'stocking_date' => 'date',
            'avg_weight_g' => 'decimal:2',
            'cost_per_fingerling' => 'decimal:2',
            'transport_cost' => 'decimal:2',
        ];
    }

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

    public function cycle(): BelongsTo
    {
        return $this->belongsTo(ProductionCycle::class, 'cycle_id');
    }

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

    public function getTotalWeightKgAttribute(): float
    {
        return $this->quantity * (float) $this->avg_weight_g / 1000;
    }

    public function getTotalFinglerlingCostAttribute(): float
    {
        return $this->quantity * (float) $this->cost_per_fingerling;
    }

    public function getTotalCostAttribute(): float
    {
        return $this->total_finglerling_cost + (float) $this->transport_cost;
    }
}
