<?php

declare(strict_types=1);

namespace App\Repositories;

final class InstructorRepository extends BaseRepository
{
    public function allForTenant(int $tenantId): array
    {
        $statement = $this->db->prepare('SELECT * FROM instructors WHERE tenant_id = :tenant_id ORDER BY last_name, first_name');
        $statement->execute(['tenant_id' => $tenantId]);

        return $statement->fetchAll();
    }

    public function findByUserId(int $tenantId, int $userId): ?array
    {
        $statement = $this->db->prepare(
            'SELECT * FROM instructors WHERE tenant_id = :tenant_id AND user_id = :user_id LIMIT 1'
        );
        $statement->execute(['tenant_id' => $tenantId, 'user_id' => $userId]);

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

    public function create(int $tenantId, array $data): array
    {
        $timestamp = $this->now();
        $statement = $this->db->prepare(
            'INSERT INTO instructors
            (tenant_id, user_id, first_name, last_name, color, notes, is_active, pre_start_buffer_minutes, created_at, updated_at)
            VALUES (:tenant_id, :user_id, :first_name, :last_name, :color, :notes, :is_active, :pre_start_buffer_minutes, :created_at, :updated_at)'
        );
        $statement->execute([
            'tenant_id' => $tenantId,
            'user_id' => $data['user_id'] ?? null,
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'color' => $data['color'],
            'notes' => $data['notes'] ?? '',
            'is_active' => !empty($data['is_active']) ? 1 : 0,
            'pre_start_buffer_minutes' => max(0, (int) ($data['pre_start_buffer_minutes'] ?? 0)),
            'created_at' => $timestamp,
            'updated_at' => $timestamp,
        ]);

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

    public function update(int $tenantId, int $id, array $data): ?array
    {
        $statement = $this->db->prepare(
            'UPDATE instructors
             SET user_id = :user_id, first_name = :first_name, last_name = :last_name, color = :color,
                 notes = :notes, is_active = :is_active, pre_start_buffer_minutes = :pre_start_buffer_minutes, updated_at = :updated_at
             WHERE tenant_id = :tenant_id AND id = :id'
        );
        $statement->execute([
            'tenant_id' => $tenantId,
            'id' => $id,
            'user_id' => $data['user_id'] ?? null,
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'color' => $data['color'],
            'notes' => $data['notes'] ?? '',
            'is_active' => !empty($data['is_active']) ? 1 : 0,
            'pre_start_buffer_minutes' => max(0, (int) ($data['pre_start_buffer_minutes'] ?? 0)),
            'updated_at' => $this->now(),
        ]);

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

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

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