<?php

namespace App\Models;

use App\Traits\HasSlugHistory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;

class Manufacturer extends Model
{
    use HasFactory, HasSlugHistory, SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<string>
     */
    protected $fillable = [
        'name',
        'slug',
        'website',
        'headquarters',
        'description',
        'total_rides',
        'total_roller_coasters',
        'is_active',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'total_rides' => 'integer',
        'total_roller_coasters' => 'integer',
        'is_active' => 'boolean',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

    /**
     * Get the rides manufactured by this company.
     */
    public function rides(): HasMany
    {
        return $this->hasMany(Ride::class, 'manufacturer_id');
    }

    /**
     * Update ride statistics.
     */
    public function updateStatistics(): void
    {
        $this->total_rides = $this->rides()->count();
        $this->total_roller_coasters = $this->rides()
            ->where('category', 'RC')
            ->count();
        $this->save();
    }

    /**
     * Get the manufacturer's name with total rides.
     */
    public function getDisplayNameAttribute(): string
    {
        return "{$this->name} ({$this->total_rides} rides)";
    }

    /**
     * Get formatted website URL (ensures proper URL format).
     */
    public function getWebsiteUrlAttribute(): string
    {
        if (!$this->website) {
            return '';
        }

        $website = $this->website;
        if (!str_starts_with($website, 'http://') && !str_starts_with($website, 'https://')) {
            $website = 'https://' . $website;
        }

        return $website;
    }

    /**
     * Scope a query to only include major manufacturers (with multiple rides).
     */
    public function scopeMajorManufacturers($query, int $minRides = 5)
    {
        return $query->where('total_rides', '>=', $minRides);
    }

    /**
     * Scope a query to only include coaster manufacturers.
     */
    public function scopeCoasterManufacturers($query)
    {
        return $query->where('total_roller_coasters', '>', 0);
    }

    /**
     * Scope a query to only include active manufacturers.
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Scope a query for optimized loading with statistics.
     */
    public function scopeOptimized($query)
    {
        return $query->select(['id', 'name', 'slug', 'total_rides', 'total_roller_coasters', 'is_active']);
    }

    /**
     * Get the route key for the model.
     */
    public function getRouteKeyName(): string
    {
        return 'slug';
    }
}