<?php

namespace App\Models;

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

class Vessel extends Model
{
    use HasFactory;

    protected $table = 'vessels';

    protected $fillable = [
        'owner_fisherman_id',
        'registration_number',
        'vessel_name',
        'vessel_type',
        'construction_material',
        'length',
        'width',
        'depth',
        'gross_tonnage',
        'engine_power',
        'engine_type',
        'engine_serial_number',
        'capacity_kg',
        'max_crew',
        'fishing_gear',
        'communication_equipment',
        'safety_equipment',
        'navigation_equipment',
        'inspection_certificate',
        'inspection_certificate_expiry',
        'insurance_policy_number',
        'insurance_expiry_date',
        'fishing_license_number',
        'fishing_license_expiry',
        'color',
        'photo_url',
        'registration_photo_url',
        'status',
        'notes',
        'metadata',
    ];

    protected $casts = [
        'length' => 'decimal:2',
        'width' => 'decimal:2',
        'depth' => 'decimal:2',
        'gross_tonnage' => 'decimal:2',
        'engine_power' => 'decimal:2',
        'capacity_kg' => 'decimal:2',
        'inspection_certificate_expiry' => 'date',
        'insurance_expiry_date' => 'date',
        'fishing_license_expiry' => 'date',
        'communication_equipment' => 'array',
        'safety_equipment' => 'array',
        'navigation_equipment' => 'array',
        'metadata' => 'array',
    ];

    // Status constants
    const STATUS_ACTIVE = 'active';
    const STATUS_MAINTENANCE = 'maintenance';
    const STATUS_DAMAGED = 'damaged';
    const STATUS_DECOMMISSIONED = 'decommissioned';
    const STATUS_SUSPENDED = 'suspended';

    const STATUS_LIST = [
        self::STATUS_ACTIVE => 'Ativo',
        self::STATUS_MAINTENANCE => 'Manutenção',
        self::STATUS_DAMAGED => 'Danificado',
        self::STATUS_DECOMMISSIONED => 'Descomissionado',
        self::STATUS_SUSPENDED => 'Suspenso',
    ];

    // Vessel types
    const TYPE_CANOA = 'canoa';
    const TYPE_LANCHA = 'lancha';
    const TYPE_BOTE = 'bote';
    const TYPE_TRAINEIRA = 'traineira';
    const TYPE_ARRASTAO = 'arrastão';
    const TYPE_OUTRO = 'outro';

    const TYPE_LIST = [
        self::TYPE_CANOA => 'Canoa',
        self::TYPE_LANCHA => 'Lancha',
        self::TYPE_BOTE => 'Bote',
        self::TYPE_TRAINEIRA => 'Traineira',
        self::TYPE_ARRASTAO => 'Arrastão',
        self::TYPE_OUTRO => 'Outro',
    ];

    // Construction materials
    const MATERIAL_MADEIRA = 'madeira';
    const MATERIAL_FIBRA = 'fibra';
    const MATERIAL_ALUMINIO = 'aluminio';
    const MATERIAL_ACO = 'aco';

    const MATERIAL_LIST = [
        self::MATERIAL_MADEIRA => 'Madeira',
        self::MATERIAL_FIBRA => 'Fibra',
        self::MATERIAL_ALUMINIO => 'Alumínio',
        self::MATERIAL_ACO => 'Aço',
    ];

    // Relationships
    public function owner()
    {
        return $this->belongsTo(Fisherman::class, 'owner_fisherman_id');
    }

    public function boardings()
    {
        return $this->hasMany(Boarding::class);
    }

    public function crews()
    {
        return $this->hasMany(VesselCrew::class);
    }

    public function licenses()
    {
        return $this->hasMany(FishingLicense::class);
    }

    public function infractions()
    {
        return $this->hasMany(FishingInfraction::class);
    }

    // Scopes
    public function scopeActive($query)
    {
        return $query->where('status', self::STATUS_ACTIVE);
    }

    public function scopeAvailable($query)
    {
        return $query->whereIn('status', [self::STATUS_ACTIVE, self::STATUS_MAINTENANCE]);
    }

    // Accessors
    public function getStatusTextAttribute()
    {
        return self::STATUS_LIST[$this->status] ?? $this->status;
    }

    public function getVesselTypeTextAttribute()
    {
        return self::TYPE_LIST[$this->vessel_type] ?? $this->vessel_type;
    }

    public function getConstructionMaterialTextAttribute()
    {
        return self::MATERIAL_LIST[$this->construction_material] ?? $this->construction_material;
    }

    public function getIsLicenseValidAttribute()
    {
        return $this->fishing_license_expiry && $this->fishing_license_expiry->isFuture();
    }

    public function getIsInspectionValidAttribute()
    {
        return $this->inspection_certificate_expiry && $this->inspection_certificate_expiry->isFuture();
    }

    public function getIsInsuranceValidAttribute()
    {
        return $this->insurance_expiry_date && $this->insurance_expiry_date->isFuture();
    }

    // Methods
    public function isActive()
    {
        return $this->status === self::STATUS_ACTIVE;
    }

    public function canSail()
    {
        return $this->isActive() && $this->isLicenseValid && $this->isInspectionValid;
    }
}
