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

    protected $fillable = [
        'user_id',
        'tank_id',
        'code',
        'fish_count_initial',
        'fish_count_current',
        'avg_weight_initial_g',
        'avg_weight_current_g',
        'biomass_current_kg',
        'status',
        'feeding_plan_snapshot',
        'batch_plan_history',
        'costs_accumulated_crc',
        'revenue_crc',
        'notes',
        'started_at',
        'ended_at',
    ];

    protected $casts = [
        'started_at' => 'date',
        'ended_at' => 'date',
        'feeding_plan_snapshot' => 'array',
        'batch_plan_history' => 'array',
    ];

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

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

    public function splitEventsOut(): HasMany
    {
        return $this->hasMany(BatchSplitEvent::class, 'source_batch_id');
    }

    public function splitEventsIn(): HasMany
    {
        return $this->hasMany(BatchSplitEvent::class, 'destination_batch_id');
    }
}
