# Buildings & Units

Buildings and units are the core inventory of the platform.

## Building model

`App\Models\Building` represents a physical property owned by an owner.

Key fields:

- `owner_id`
- `region_id`
- `currency_id`
- `name`
- `check_in_time` / `check_out_time`
- `map_lat` / `map_lng`
- `status` — `active`, `inactive`, or `hidden`
- `slug` (unique)
- `payment_methods` (JSON)
- `booking_conditions` (JSON)

Relationships:

- `owner`
- `region`
- `currency`
- `units`
- `facilities` (BelongsToMany)
- media photos (Spatie MediaLibrary)

## Unit model

`App\Models\Unit` represents a rentable space within a building.

Key fields:

- `building_id`
- `floor`
- `name_or_number`
- `rooms`
- `base_price`
- `offer_price`
- `guest_type`
- `max_adults` / `max_children` / `max_child_age`
- `status`
- `slug` (unique)
- `booking_conditions` (JSON)

Relationships:

- `building`
- `reservations`
- `availabilities`
- `facilities` (BelongsToMany)
- media photos

## Facilities

`App\Models\Facility` represents amenities such as Wi-Fi, parking, or a pool. Facilities can be attached to buildings and units.

## Availability ledger

`App\Models\UnitAvailability` stores one row per unit per night.

Fields:

- `unit_id`
- `date`
- `status` — `available`, `blocked`, or `booked`
- `reservation_id`
- `pending_reservation_id`

This ledger enables reliable date-range conflict detection. When a reservation is confirmed, the matching rows become `booked`. When canceled, they revert to `available`. Owners can `block` dates to take units off the market.

## Public catalog

Unauthenticated users can list and view units. The catalog supports filters for:

- Country, city, region
- Price range
- Guest counts
- Facilities
- Dates (availability check)

## Bulk unit creation

Owners can bulk-create units under a building. This is useful for hotels or apartment blocks with many similar units.

## Photo uploads

Buildings and units support photo uploads via Spatie MediaLibrary. The `HandlesMediaPhotos` trait centralizes upload and replacement logic. Allowed formats are typically JPG, JPEG, PNG, and WebP with size and dimension limits.
