<?php

declare(strict_types=1);

namespace App\DataTransferObjects;

final readonly class PropertyData
{
    public function __construct(
        public int $ownerId,
        public string $title,
        public string $description,
        public string $propertyType,
        public int $bedrooms,
        public int $bathrooms,
        public int $beds,
        public int $maxGuests,
        public string $address,
        public string $city,
        public string $state,
        public string $country,
        public string $postalCode,
        public ?float $latitude,
        public ?float $longitude,
        public float $pricePerNight,
        public float $cleaningFee,
        public float $serviceFee,
        public int $minimumNights,
        public int $maximumNights,
        public bool $instantBook,
        public string $status = 'draft',
        public bool $featured = false,
        public array $amenityIds = [],
    ) {}

    /**
     * Create from array (e.g., validated request data)
     *
     * @param  array<string, mixed>  $data
     */
    public static function fromArray(array $data): self
    {
        return new self(
            ownerId: (int) $data['owner_id'],
            title: $data['title'],
            description: $data['description'],
            propertyType: $data['property_type'],
            bedrooms: (int) $data['bedrooms'],
            bathrooms: (int) $data['bathrooms'],
            beds: (int) $data['beds'],
            maxGuests: (int) $data['max_guests'],
            address: $data['address'],
            city: $data['city'],
            state: $data['state'],
            country: $data['country'],
            postalCode: $data['postal_code'],
            latitude: isset($data['latitude']) ? (float) $data['latitude'] : null,
            longitude: isset($data['longitude']) ? (float) $data['longitude'] : null,
            pricePerNight: (float) $data['price_per_night'],
            cleaningFee: (float) ($data['cleaning_fee'] ?? 0),
            serviceFee: (float) ($data['service_fee'] ?? 0),
            minimumNights: (int) ($data['minimum_nights'] ?? 1),
            maximumNights: (int) ($data['maximum_nights'] ?? 30),
            instantBook: (bool) ($data['instant_book'] ?? false),
            status: $data['status'] ?? 'draft',
            featured: (bool) ($data['featured'] ?? false),
            amenityIds: $data['amenity_ids'] ?? [],
        );
    }

    /**
     * Convert to array for model creation
     *
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'owner_id' => $this->ownerId,
            'title' => $this->title,
            'description' => $this->description,
            'property_type' => $this->propertyType,
            'bedrooms' => $this->bedrooms,
            'bathrooms' => $this->bathrooms,
            'beds' => $this->beds,
            'max_guests' => $this->maxGuests,
            'address' => $this->address,
            'city' => $this->city,
            'state' => $this->state,
            'country' => $this->country,
            'postal_code' => $this->postalCode,
            'latitude' => $this->latitude,
            'longitude' => $this->longitude,
            'price_per_night' => $this->pricePerNight,
            'cleaning_fee' => $this->cleaningFee,
            'service_fee' => $this->serviceFee,
            'minimum_nights' => $this->minimumNights,
            'maximum_nights' => $this->maximumNights,
            'instant_book' => $this->instantBook,
            'status' => $this->status,
            'featured' => $this->featured,
        ];
    }
}
