<?php

declare(strict_types=1);

namespace Kylin\Agriculture\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * 地块模型
 *
 * 智慧农业地块管理，记录农田地块信息和传感器绑定。
 */
class Plot extends Model
{
    protected $table = 'kylin_agri_plots';

    protected $fillable = [
        'name',
        'area',
        'type',
        'soil_type',
        'irrigation',
        'longitude',
        'latitude',
        'manager_id',
        'status',
    ];

    protected function casts(): array
    {
        return [
            'area' => 'decimal:2',
            'longitude' => 'decimal:7',
            'latitude' => 'decimal:7',
            'status' => 'boolean',
        ];
    }

    public function manager(): BelongsTo
    {
        return $this->belongsTo(config('auth.providers.users.model'), 'manager_id');
    }

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

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

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

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

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

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