<?php

namespace App\Models;

use Carbon\Carbon;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Pond extends Model
{
    use HasFactory, Sluggable;

    const PROCESS      = 1;
    const HARVEST      = 2;
    const NONACTIVE    = 3;

    protected $table='ponds';

    protected $fillable=[
        'user_id',
        'fish_id',
        'name',
        'slug',
        'sow_date',
        'seed_amount',
        'seed_weight',
        'survival_rate',
        'volume_pond',
        'average_weight',
        'total_feed_spent',
        'prediction',
        'status',
    ];

    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'name'
            ]
        ];
    }

    public function user() {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function fish() {
        return $this->belongsTo(FishType::class, 'fish_id');
    }

    public function cultivations() {
        return $this->hasMany(Cultivation::class, 'pond_id');
    }

    public function getDateSowAttribute()
    {
        $date_sow = $this->sow_date ? Carbon::parse($this->sow_date)->locale('id')->isoFormat('D MMMM Y') : Carbon::parse(now())->locale('id')->isoFormat('D MMMM Y');
        return $date_sow;
    }

    public function getHarvestDateAttribute()
    {
        $add = $this->fish->duration - 1;
        $harvest_date = Carbon::parse($this->sow_date)->addDays($add)->locale('id')->isoFormat('D MMMM Y');
        return $harvest_date;
    }
}
