<?php

namespace App\Models;

use App\Services\AgeCheckerService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Batches extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = "batches";

    protected $fillable = [
        "farm_id",
        "farm_name",
        "building_id",
        "building_name",
        "batch_no",
        "batch_sequence",
        "start_date",
        "end_date",
        "status",
        "synced_status",
        "synced_at",
        "synced_id",
        "initial_population",
        "current_population",
        "total_culled",
        "total_mortality",
        "total_harvested",
        "attachment_path",
        "attachment_uploaded_at",
    ];

    public function farm()
    {
        return $this->belongsTo(Farm::class, "farm_id", "id");
    }

    public function building()
    {
        return $this->belongsTo(Building::class, "building_id", "id");
    }

    public function loading()
    {
        return $this->hasOne(ChickenLoadingHeaders::class, "batch_id", "id");
    }

    public function transactions()
    {
        return $this->hasMany(Transaction::class, "batch_id", "id");
    }

    protected $appends = ['age', 'classification'];

    private function getAgeData(): array
    {
        return (new AgeCheckerService())->check($this->start_date);
    }

    public function getAgeAttribute()
    {
        return $this->getAgeData()['age'];
    }

    public function getClassificationAttribute()
    {
        return $this->getAgeData()['classification'];
    }
}
