<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class BatchKpiSnapshot extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'batch_id',
        'tank_id',
        'batch_status',
        'snapshot_date',
        'cycle_day',
        'stage_code',
        'last_split_date',
        'stage_days_elapsed',
        'uniformity_cv_pct',
        'weight_spread_g',
        'days_since_last_split',
        'post_split_biomass_gain_kg',
        'post_split_feed_kg',
        'post_split_fcr_real',
        'stage_biomass_gain_kg',
        'stage_feed_kg',
        'stage_fcr_real',
        'stage_fcr_target',
        'stage_fcr_gap',
        'post_split_fcr_target',
        'post_split_fcr_gap',
        'fish_alive',
        'avg_weight_g',
        'biomass_kg',
        'total_cost_crc',
        'realized_revenue_crc',
        'harvested_kg_cumulative',
        'sold_kg_cumulative',
        'cash_recovery_pct',
        'capital_locked_days',
        'cycle_closed_at',
        'harvest_completion_pct',
        'sales_realization_pct',
        'cycle_closure_ready',
        'cycle_closure_state',
        'target_progress_pct',
        'net_cashflow_crc',
        'roi_pct',
        'projected_roi_pct',
        'cost_per_kg_crc',
        'projected_revenue_crc',
        'projected_margin_crc',
        'margin_per_kg_crc',
        'break_even_weight_g',
        'days_to_target_estimated',
        'status',
        'growth_rate_g_per_day',
        'mortality_rate_pct',
        'hidden_loss_fish',
        'hidden_loss_biomass_kg',
        'recommended_feed_g',
        'actual_feed_g',
        'feed_variance_g',
        'cumulative_feed_g',
        'cumulative_feed_kg',
        'feed_conversion_ratio_real',
        'density_fish_m3',
        'density_biomass_kg_m3',
    ];

    protected $casts = [
        'snapshot_date' => 'date:Y-m-d',
        'last_split_date' => 'date:Y-m-d',
        'cycle_closed_at' => 'date:Y-m-d',
        'avg_weight_g' => 'float',
        'uniformity_cv_pct' => 'float',
        'weight_spread_g' => 'float',
        'realized_revenue_crc' => 'float',
        'harvested_kg_cumulative' => 'float',
        'sold_kg_cumulative' => 'float',
        'cash_recovery_pct' => 'float',
        'capital_locked_days' => 'float',
        'harvest_completion_pct' => 'float',
        'sales_realization_pct' => 'float',
        'cycle_closure_ready' => 'boolean',
        'target_progress_pct' => 'float',
        'net_cashflow_crc' => 'float',
        'roi_pct' => 'float',
        'projected_roi_pct' => 'float',
        'post_split_biomass_gain_kg' => 'float',
        'post_split_feed_kg' => 'float',
        'post_split_fcr_real' => 'float',
        'stage_biomass_gain_kg' => 'float',
        'stage_feed_kg' => 'float',
        'stage_fcr_real' => 'float',
        'stage_fcr_target' => 'float',
        'stage_fcr_gap' => 'float',
        'post_split_fcr_target' => 'float',
        'post_split_fcr_gap' => 'float',
        'biomass_kg' => 'float',
        'total_cost_crc' => 'float',
        'cost_per_kg_crc' => 'float',
        'projected_revenue_crc' => 'float',
        'projected_margin_crc' => 'float',
        'margin_per_kg_crc' => 'float',
        'break_even_weight_g' => 'float',
        'growth_rate_g_per_day' => 'float',
        'mortality_rate_pct' => 'float',
        'hidden_loss_biomass_kg' => 'float',
        'recommended_feed_g' => 'float',
        'actual_feed_g' => 'float',
        'feed_variance_g' => 'float',
        'cumulative_feed_g' => 'float',
        'cumulative_feed_kg' => 'float',
        'feed_conversion_ratio_real' => 'float',
        'density_fish_m3' => 'float',
        'density_biomass_kg_m3' => 'float',
    ];

    public function batch(): BelongsTo
    {
        return $this->belongsTo(Batch::class, 'batch_id');
    }

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