<?php

declare(strict_types=1);

namespace App\DataTransferObjects;

final readonly class PropertyFilterData
{
    public function __construct(
        public ?string $city = null,
        public ?string $state = null,
        public ?string $country = null,
        public ?string $propertyType = null,
        public ?float $minPrice = null,
        public ?float $maxPrice = null,
        public ?int $minBedrooms = null,
        public ?int $minBathrooms = null,
        public ?int $guests = null,
        public ?bool $instantBook = null,
        public ?bool $featured = null,
        public array $amenities = [],
        public ?string $sortBy = 'created_at',
        public ?string $sortDirection = 'desc',
    ) {}

    /**
     * Create from array (e.g., request data)
     *
     * @param  array<string, mixed>  $data
     */
    public static function fromArray(array $data): self
    {
        return new self(
            city: $data['city'] ?? null,
            state: $data['state'] ?? null,
            country: $data['country'] ?? null,
            propertyType: $data['property_type'] ?? null,
            minPrice: isset($data['min_price']) ? (float) $data['min_price'] : null,
            maxPrice: isset($data['max_price']) ? (float) $data['max_price'] : null,
            minBedrooms: isset($data['min_bedrooms']) ? (int) $data['min_bedrooms'] : null,
            minBathrooms: isset($data['min_bathrooms']) ? (int) $data['min_bathrooms'] : null,
            guests: isset($data['guests']) ? (int) $data['guests'] : null,
            instantBook: isset($data['instant_book']) ? (bool) $data['instant_book'] : null,
            featured: isset($data['featured']) ? (bool) $data['featured'] : null,
            amenities: $data['amenities'] ?? [],
            sortBy: $data['sort_by'] ?? 'created_at',
            sortDirection: $data['sort_direction'] ?? 'desc',
        );
    }

    /**
     * Convert to array for repository
     *
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return array_filter([
            'city' => $this->city,
            'state' => $this->state,
            'country' => $this->country,
            'property_type' => $this->propertyType,
            'min_price' => $this->minPrice,
            'max_price' => $this->maxPrice,
            'min_bedrooms' => $this->minBedrooms,
            'min_bathrooms' => $this->minBathrooms,
            'guests' => $this->guests,
            'instant_book' => $this->instantBook,
            'featured' => $this->featured,
            'amenities' => $this->amenities,
        ], fn ($value) => $value !== null);
    }

    /**
     * Check if any filters are active
     */
    public function hasFilters(): bool
    {
        return $this->city !== null
            || $this->state !== null
            || $this->country !== null
            || $this->propertyType !== null
            || $this->minPrice !== null
            || $this->maxPrice !== null
            || $this->minBedrooms !== null
            || $this->minBathrooms !== null
            || $this->guests !== null
            || $this->instantBook !== null
            || $this->featured !== null
            || ! empty($this->amenities);
    }
}
