<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Str;

class Package extends Model
{
    use HasFactory;

    protected $fillable = [
        'title',
        'slug',
        'description',
        'itinerary',
        'price',
        'currency',
        'duration_days',
        'destination_id',
        'category',
        'max_people',
        'min_people',
        'is_active',
        'is_featured',
        'images',
        'inclusions',
        'exclusions',
        'highlights',
        'discount_price',
        'start_date',
        'end_date',
        'difficulty_level',
        'accommodation_type',
        'transportation',
        'meeting_point',
        'what_to_bring',
        'available_slots',

        // SEO fields
        'seo_title',
        'seo_description',
        'seo_keywords',
        'seo_og_image',
    ];

    protected $casts = [
        'price' => 'decimal:2',
        'discount_price' => 'decimal:2',
        'duration_days' => 'integer',
        'max_people' => 'integer',
        'min_people' => 'integer',
        'is_active' => 'boolean',
        'is_featured' => 'boolean',
        'images' => 'array',
        'inclusions' => 'array',
        'exclusions' => 'array',
        'highlights' => 'array',
        'start_date' => 'date',
        'end_date' => 'date',
        'available_slots' => 'integer',
    ];

    public function destination()
    {
        return $this->belongsTo(Destination::class);
    }

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

    public function confirmedBookings(): HasMany
    {
        return $this->hasMany(Booking::class)->where('status', 'confirmed');
    }

    public function getFormattedPriceAttribute(): string
    {
        return number_format($this->price, 2) . ' ' . $this->currency;
    }

    public function getFormattedDiscountPriceAttribute(): ?string
    {
        return $this->discount_price 
            ? number_format($this->discount_price, 2) . ' ' . $this->currency 
            : null;
    }

    public function getHasDiscountAttribute(): bool
    {
        return $this->discount_price && $this->discount_price < $this->price;
    }

    public function getDiscountPercentageAttribute(): ?float
    {
        if (!$this->has_discount) {
            return null;
        }
        
        return round((($this->price - $this->discount_price) / $this->price) * 100, 1);
    }

    public function getMainImageAttribute(): ?string
    {
        $images = $this->images;
        return $images && !empty($images) ? $images[0] : null;
    }

    public function getIsAvailableAttribute(): bool
    {
        return $this->is_active && 
               (!$this->available_slots || $this->available_slots > 0) &&
               (!$this->end_date || $this->end_date >= now());
    }

    public function getAvailableSlotsCountAttribute(): int
    {
        if ($this->available_slots === null) {
            return $this->max_people;
        }
        
        $confirmedCount = $this->confirmedBookings()->sum('number_of_people');
        return max(0, $this->available_slots - $confirmedCount);
    }

    protected static function boot()
    {
        parent::boot();
        
        static::creating(function ($package) {
            if (empty($package->slug)) {
                $package->slug = Str::slug($package->title);
            }
        });
        
        static::updating(function ($package) {
            if ($package->isDirty('title') && empty($package->slug)) {
                $package->slug = Str::slug($package->title);
            }
        });
    }
}
