<?php

declare(strict_types=1);

namespace App\Repositories;

final class InstructorBreakRuleRepository extends BaseRepository
{
    public function getForInstructor(int $instructorId): array
    {
        $stmt = $this->db->prepare(
            'SELECT * FROM instructor_break_rules
             WHERE instructor_id = :instructor_id
             ORDER BY condition_type, threshold'
        );
        $stmt->execute(['instructor_id' => $instructorId]);
        return $stmt->fetchAll();
    }

    public function getActiveForInstructor(int $instructorId): array
    {
        $stmt = $this->db->prepare(
            'SELECT * FROM instructor_break_rules
             WHERE instructor_id = :instructor_id AND is_active = 1
             ORDER BY condition_type, threshold'
        );
        $stmt->execute(['instructor_id' => $instructorId]);
        return $stmt->fetchAll();
    }

    public function create(
        int $instructorId,
        string $conditionType,
        string $operator,
        float $threshold,
        int $breakMinutes,
        bool $isActive = true
    ): array {
        $stmt = $this->db->prepare(
            'INSERT INTO instructor_break_rules
             (instructor_id, condition_type, operator, threshold, break_minutes, is_active)
             VALUES (:instructor_id, :condition_type, :operator, :threshold, :break_minutes, :is_active)
             RETURNING *'
        );
        $stmt->execute([
            'instructor_id' => $instructorId,
            'condition_type' => $conditionType,
            'operator' => $operator,
            'threshold' => $threshold,
            'break_minutes' => $breakMinutes,
            'is_active' => $isActive ? 1 : 0,
        ]);
        return $stmt->fetch();
    }

    public function update(
        int $id,
        string $conditionType,
        string $operator,
        float $threshold,
        int $breakMinutes,
        bool $isActive
    ): bool {
        $stmt = $this->db->prepare(
            'UPDATE instructor_break_rules
             SET condition_type = :condition_type, operator = :operator,
                 threshold = :threshold, break_minutes = :break_minutes, is_active = :is_active
             WHERE id = :id RETURNING id'
        );
        $stmt->execute([
            'id' => $id,
            'condition_type' => $conditionType,
            'operator' => $operator,
            'threshold' => $threshold,
            'break_minutes' => $breakMinutes,
            'is_active' => $isActive ? 1 : 0,
        ]);
        return (bool) $stmt->fetch();
    }

    public function delete(int $id): bool
    {
        $stmt = $this->db->prepare('DELETE FROM instructor_break_rules WHERE id = :id RETURNING id');
        $stmt->execute(['id' => $id]);
        return (bool) $stmt->fetch();
    }

    public function setActive(int $id, bool $isActive): bool
    {
        $stmt = $this->db->prepare(
            'UPDATE instructor_break_rules SET is_active = :is_active WHERE id = :id RETURNING id'
        );
        $stmt->execute(['id' => $id, 'is_active' => $isActive ? 1 : 0]);
        return (bool) $stmt->fetch();
    }
}