<?php

namespace App\Models\Tenant;

use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class TireStorage extends Model
{
    use BelongsToTenant;

    protected $table = 'tire_storage';

    public const STATUSES = [
        'stored' => 'În depozit',
        'retrieved' => 'Ridicat',
    ];

    protected $fillable = [
        'company_id', 'tire_set_id',
        'location', 'season_label', 'status',
        'checked_in_at', 'checked_out_at', 'fee', 'paid', 'notes',
    ];

    protected $casts = [
        'checked_in_at' => 'datetime',
        'checked_out_at' => 'datetime',
        'fee' => 'decimal:2',
        'paid' => 'boolean',
    ];

    public function tireSet(): BelongsTo
    {
        return $this->belongsTo(TireSet::class);
    }

    public function isActive(): bool
    {
        return $this->status === 'stored';
    }

    public function durationDays(): int
    {
        $end = $this->checked_out_at ?? now();
        return (int) $this->checked_in_at?->diffInDays($end);
    }
}
