# Employee ↔ Many Buildings

## Goal
Allow an employee to be assigned to more than one building.

## Schema changes

1. Create pivot table `building_employee`:
   - `employee_id` (FK to `employees.id`, cascade on delete)
   - `building_id` (FK to `buildings.id`, cascade on delete)
   - Unique composite index on `[employee_id, building_id]`
   - Timestamps optional
2. In the same migration, copy existing `employees.building_id` values into the pivot table.
3. Drop the `building_id` column from `employees`.

The migration must work with `php artisan migrate` on existing data; `migrate:fresh` is not required.

## Model changes

- `Employee`:
  - Remove `building(): BelongsTo`
  - Add `buildings(): BelongsToMany<Building>`
- `Building` (optional):
  - Add `employees(): BelongsToMany<Employee>` for symmetry.

## API changes

### Requests

- `StoreEmployeeRequest` and `UpdateEmployeeRequest` accept both:
  - `building_id` — nullable integer, legacy single-building assignment
  - `building_ids` — nullable array of integer IDs
- Normalize both fields into one array of IDs.
- Validate every ID with `exists:buildings,id` and owner-scope check (must belong to the authenticated owner/employee's owner).
- `UpdateEmployeeRequest` continues to prohibit employees from changing their own `status`.

### Controller

- `EmployeeController::store`:
  - Create user + employee as before.
  - Sync pivot table with the normalized building IDs.
- `EmployeeController::update`:
  - Update user/employee scalar fields.
  - Sync pivot table when building input is present.
- `EmployeeController::index`:
  - `building_id=unassigned` → employees with zero buildings.
  - `building_id=<id>` → employees linked to that building via pivot.

### Resource

- `EmployeeResource` returns:
  - `building_ids` — array of assigned building IDs
  - `buildings` — minimal list (`id`, `name`)
- Remove `building_id` from the response because it is no longer accurate.

## Backward compatibility

- Clients sending `building_id` (single) keep working.
- New clients can send `building_ids` (array).
- Sending both merges them and deduplicates.

## Tests

Extend existing feature tests:

- `EmployeeStoreTest` — single `building_id`, multiple `building_ids`, cross-owner rejection.
- `EmployeeUpdateTest` — add/remove multiple buildings, cross-owner rejection.
- `EmployeeListFiltersTest` — filter by one building, filter by unassigned, only returns employees linked to that building.

## Verification

- Full Pest suite passes.
- Laravel Pint passes.
- `php artisan migrate` works on existing database without data loss.
