<?php

namespace App\Models\Product;

use App\Models\Agency\Agency;
use App\Models\City;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class DayTripTour extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'agency_id',
        'product_id',
        'code',
        'name',
        'slug',
        'description',
        'short_description',
        'highlights',
        'inclusions',
        'exclusions',
        'departure_time',
        'return_time',
        'duration_hours',
        'departure_city_id',
        'destination_city_id',
        'visit_locations',
        'distance_km',
        'transportation_type',
        'has_guide',
        'languages',
        'min_participants',
        'max_participants',
        'min_age',
        'child_friendly',
        'meal_included',
        'meal_type',
        'beverages_included',
        'entrance_fees_included',
        'difficulty_level',
        'base_price_adult',
        'base_price_child',
        'base_price_infant',
        'currency',
        'instant_booking',
        'booking_deadline_hours',
        'cancellation_deadline_hours',
        'cancellation_policy',
        'video_url',
        'documents',
        'meta_title',
        'meta_description',
        'tags',
        'is_active',
        'is_bookable',
        'is_featured',
        'published_at',
        'view_count',
        'rating_average',
        'rating_count',
        'created_by',
        'updated_by',
    ];

    protected $casts = [
        'visit_locations' => 'array',
        'languages' => 'array',
        'has_guide' => 'boolean',
        'child_friendly' => 'boolean',
        'meal_included' => 'boolean',
        'beverages_included' => 'boolean',
        'entrance_fees_included' => 'boolean',
        'instant_booking' => 'boolean',
        'documents' => 'array',
        'tags' => 'array',
        'is_active' => 'boolean',
        'is_bookable' => 'boolean',
        'is_featured' => 'boolean',
        'published_at' => 'datetime',
        'departure_time' => 'datetime:H:i',
        'return_time' => 'datetime:H:i',
        'base_price_adult' => 'decimal:2',
        'base_price_child' => 'decimal:2',
        'base_price_infant' => 'decimal:2',
        'rating_average' => 'decimal:2',
        'distance_km' => 'decimal:2',
    ];

    // Polymorphic relationship
    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    public function agency()
    {
        return $this->belongsTo(Agency::class);
    }

    public function departureCity()
    {
        return $this->belongsTo(City::class, 'departure_city_id');
    }

    public function destinationCity()
    {
        return $this->belongsTo(City::class, 'destination_city_id');
    }

    public function dates()
    {
        return $this->hasMany(DayTripTourDate::class);
    }

    public function transferPoints()
    {
        return $this->hasMany(DayTripTourTransferPoint::class)->orderBy('sort_order');
    }

    public function images()
    {
        return $this->hasMany(DayTripTourImage::class)->orderBy('sort_order');
    }

    public function coverImage()
    {
        return $this->hasOne(DayTripTourImage::class)->where('is_cover', true);
    }

    public function featuredImages()
    {
        return $this->hasMany(DayTripTourImage::class)->where('is_featured', true);
    }

    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function updater()
    {
        return $this->belongsTo(User::class, 'updated_by');
    }

    // Scopes
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeBookable($query)
    {
        return $query->where('is_bookable', true)->where('is_active', true);
    }

    public function scopeFeatured($query)
    {
        return $query->where('is_featured', true);
    }

    public function scopeByAgency($query, $agencyId)
    {
        return $query->where('agency_id', $agencyId);
    }

    // Helper methods
    public function getRouteKeyName()
    {
        return 'slug';
    }

    public function isActive(): bool
    {
        return $this->is_active;
    }

    public function isBookable(): bool
    {
        return $this->is_bookable && $this->is_active;
    }

    public function getBasePrice(): float
    {
        return (float) $this->base_price_adult;
    }

    public function incrementViewCount(): void
    {
        $this->increment('view_count');
    }

    public function hasAvailableDates(): bool
    {
        return $this->dates()->where('is_bookable', true)
            ->where('tour_date', '>=', now())
            ->exists();
    }
}
