<?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;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class VineyardRow extends Model
{
    use HasFactory;

    protected $table = 'vineyard_rows';

    protected $fillable = [
        'variety_variation_id',
        'vine_count',
        'planting_year',
        'area',
        'location',
        'status',
        'notes',
    ];

    protected $casts = [
        'area' => 'decimal:2',
    ];

    /**
     * Get the variety variation for this vineyard row.
     */
    public function varietyVariation(): BelongsTo
    {
        return $this->belongsTo(VarietyVariation::class, 'variety_variation_id');
    }

    /**
     * Get the harvests for this vineyard row.
     */
    public function harvests(): HasMany
    {
        return $this->hasMany(Harvest::class, 'vineyard_row_id');
    }

    /**
     * Get the treatments assigned to this vineyard row.
     */
    public function treatments(): HasMany
    {
        return $this->hasMany(Treatment::class, 'row_id');
    }

    /**
     * Get pesticide sprayings associated with the vineyard row.
     */
    public function sprayings(): HasManyThrough
    {
        return $this->hasManyThrough(
            Spraying::class,
            Treatment::class,
            'row_id',
            'treatment_id',
            'id',
            'treatment_id'
        );
    }
}
