<?php

namespace App\Data;

use App\Helpers\UrlHelper;
use Spatie\LaravelData\Data;

class PropertyData extends Data
{
    public function __construct(
        public int $property_type, // 1=house, 2=apartment, 3=room, 4=hostel, 5=office
        public string $title,
        public ?string $description = null,
        public float $monthly_rent,
        public float $deposit_amount,
        public string $address,
        public ?float $latitude = null,
        public ?float $longitude = null,
        public ?int $bedrooms = null,
        public ?int $bathrooms = null,
        public ?int $size_sqm = null,
        public bool $is_furnished = false,
        public int $status = 3, // 1=available, 2=occupied, 3=pending, 4=maintenance
        public bool $is_verified = false,
        public ?array $images = null, // Images array for property creation
        public ?HostelData $hostel = null, // Hostel specific data
    ) {}

    public static function fromProperty(\App\Models\Property $property): self
    {
        return new self(
            property_type: $property->property_type,
            title: $property->title,
            description: $property->description,
            monthly_rent: $property->monthly_rent,
            deposit_amount: $property->deposit_amount,
            address: $property->address,
            latitude: $property->latitude,
            longitude: $property->longitude,
            bedrooms: $property->bedrooms,
            bathrooms: $property->bathrooms,
            size_sqm: $property->size_sqm,
            is_furnished: $property->is_furnished,
            status: $property->status,
            is_verified: $property->is_verified,
            images: $property->images->map(fn($img) => [
                'id' => $img->id,
                'image_url' => UrlHelper::toFullUrl($img->image_url),
                'is_primary' => $img->is_primary,
            ])->toArray(),
            hostel: $property->hostel ? HostelData::from($property->hostel) : null,
        );
    }

    public function toArray(): array
    {
        return [
            'property_type' => $this->property_type,
            'title' => $this->title,
            'description' => $this->description,
            'monthly_rent' => $this->monthly_rent,
            'deposit_amount' => $this->deposit_amount,
            'address' => $this->address,
            'latitude' => $this->latitude,
            'longitude' => $this->longitude,
            'bedrooms' => $this->bedrooms,
            'bathrooms' => $this->bathrooms,
            'size_sqm' => $this->size_sqm,
            'is_furnished' => $this->is_furnished,
            'status' => $this->status,
            'is_verified' => $this->is_verified,
        ];
    }
}