<?php

declare(strict_types=1);

namespace App\Repositories;

use PDO;

final class TenantRepository extends BaseRepository
{
    public function all(): array
    {
        return $this->db->query('SELECT * FROM tenants ORDER BY name')->fetchAll();
    }

    public function find(int $id): ?array
    {
        $statement = $this->db->prepare('SELECT * FROM tenants WHERE id = :id');
        $statement->execute(['id' => $id]);

        return $statement->fetch() ?: null;
    }

    public function create(array $data): array
    {
        $timestamp = $this->now();
        $statement = $this->db->prepare(
            'INSERT INTO tenants (name, timezone, federal_state, location_label, latitude, longitude, is_active, created_at, updated_at)
             VALUES (:name, :timezone, :federal_state, :location_label, :latitude, :longitude, :is_active, :created_at, :updated_at)'
        );
        $statement->execute([
            'name' => $data['name'],
            'timezone' => $data['timezone'] ?? 'Europe/Berlin',
            'federal_state' => $data['federal_state'] ?? 'BE',
            'location_label' => $data['location_label'] ?? '',
            'latitude' => $data['latitude'] !== '' ? $data['latitude'] : null,
            'longitude' => $data['longitude'] !== '' ? $data['longitude'] : null,
            'is_active' => !empty($data['is_active']) ? 1 : 0,
            'created_at' => $timestamp,
            'updated_at' => $timestamp,
        ]);

        return $this->find((int) $this->lastInsertId());
    }

    public function update(int $id, array $data): ?array
    {
        $statement = $this->db->prepare(
            'UPDATE tenants
             SET name = :name, timezone = :timezone, federal_state = :federal_state, location_label = :location_label,
                 latitude = :latitude, longitude = :longitude, is_active = :is_active, updated_at = :updated_at
             WHERE id = :id'
        );
        $statement->execute([
            'id' => $id,
            'name' => $data['name'],
            'timezone' => $data['timezone'],
            'federal_state' => $data['federal_state'] ?? 'BE',
            'location_label' => $data['location_label'] ?? '',
            'latitude' => $data['latitude'] !== '' ? $data['latitude'] : null,
            'longitude' => $data['longitude'] !== '' ? $data['longitude'] : null,
            'is_active' => !empty($data['is_active']) ? 1 : 0,
            'updated_at' => $this->now(),
        ]);

        return $this->find($id);
    }

    public function updateForTenantAdmin(int $id, array $data): ?array
    {
        $current = $this->find($id);
        if ($current === null) {
            return null;
        }

        return $this->update($id, [
            'name' => $data['name'] ?? $current['name'],
            'timezone' => $data['timezone'] ?? $current['timezone'],
            'federal_state' => $data['federal_state'] ?? ($current['federal_state'] ?? 'BE'),
            'location_label' => $data['location_label'] ?? ($current['location_label'] ?? ''),
            'latitude' => array_key_exists('latitude', $data) ? $data['latitude'] : ($current['latitude'] ?? null),
            'longitude' => array_key_exists('longitude', $data) ? $data['longitude'] : ($current['longitude'] ?? null),
            'is_active' => $current['is_active'],
        ]);
    }
}
