<?php

namespace App\Models\Tenant;

use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class TireSet extends Model implements HasMedia
{
    use BelongsToTenant, InteractsWithMedia, SoftDeletes;

    public const SEASONS = [
        'summer' => 'Vară',
        'winter' => 'Iarnă',
        'allseason' => 'All-season',
    ];

    public const CONDITIONS = [
        'nou' => 'Nou',
        'bun' => 'Bun',
        'uzat' => 'Uzat',
        'critic' => 'Critic',
    ];

    protected $fillable = [
        'company_id', 'client_id', 'vehicle_id',
        'label', 'season',
        'width', 'profile', 'diameter', 'brand', 'model', 'dot_year',
        'has_rims', 'rim_type',
        'tread', 'tread_min', 'tpms', 'tpms_ids',
        'condition', 'notes',
    ];

    protected $casts = [
        'tread' => 'array',
        'tpms_ids' => 'array',
        'tread_min' => 'decimal:1',
        'has_rims' => 'boolean',
        'tpms' => 'boolean',
    ];

    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('photos');
    }

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

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

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

    public function currentStorage(): ?TireStorage
    {
        return $this->storage()->where('status', 'stored')->latest('checked_in_at')->first();
    }

    public function isStored(): bool
    {
        return $this->storage()->where('status', 'stored')->exists();
    }

    public function sizeLabel(): string
    {
        if (! $this->width || ! $this->profile || ! $this->diameter) {
            return '—';
        }
        return "{$this->width}/{$this->profile} R{$this->diameter}";
    }

    /** Recompute tread_min from the per-position tread JSON. */
    public function recomputeTreadMin(): void
    {
        $vals = array_filter(array_map('floatval', array_values((array) $this->tread)), fn ($v) => $v > 0);
        $this->tread_min = $vals ? min($vals) : null;
    }

    protected static function booted(): void
    {
        static::saving(function (self $set) {
            $set->recomputeTreadMin();
        });
    }
}
