<?php

namespace App\Models;

use App\Core\Model;
use PDO;

class Batch extends Model {
    public function search($params = []) {
        $sql = "SELECT b.*, c.name as course_name, t.name as teacher_name,
                (SELECT COUNT(*) FROM admissions a WHERE a.batch_id = b.id) as students_count
                FROM batches b 
                JOIN courses c ON b.course_id = c.id 
                JOIN teachers t ON b.teacher_id = t.id 
                WHERE 1=1";
        $binds = [];

        if (!empty($params['search'])) {
            $sql .= " AND (b.name LIKE :search OR c.name LIKE :search OR t.name LIKE :search)";
            $binds['search'] = '%' . $params['search'] . '%';
        }

        if (!empty($params['filter_course'])) {
            $sql .= " AND c.name = :course_name";
            $binds['course_name'] = $params['filter_course'];
        }

        if (!empty($params['filter_status'])) {
            $sql .= " AND b.status = :status";
            $binds['status'] = $params['filter_status'];
        }

        $sql .= " ORDER BY b.id DESC";
        $stmt = $this->db->prepare($sql);
        if (!empty($binds)) {
            $stmt->execute($binds);
        } else {
            $stmt->execute();
        }
        return $stmt->fetchAll();
    }

    public function find($id) {
        $stmt = $this->db->prepare("SELECT * FROM batches WHERE id = :id LIMIT 1");
        $stmt->execute(['id' => $id]);
        $batch = $stmt->fetch();
        if ($batch) {
            $batch['days'] = explode(',', $batch['days']);
        }
        return $batch;
    }

    public function save($data) {
        if (!empty($data['id'])) {
            $sql = "UPDATE batches SET name = :name, course_id = :course_id, teacher_id = :teacher_id, 
                    start_time = :start_time, end_time = :end_time, days = :days, 
                    capacity = :capacity, status = :status WHERE id = :id";
            $stmt = $this->db->prepare($sql);
            return $stmt->execute([
                'name' => $data['name'],
                'course_id' => $data['course_id'],
                'teacher_id' => $data['teacher_id'],
                'start_time' => $data['start_time'] ?? null,
                'end_time' => $data['end_time'] ?? null,
                'days' => $data['days'] ?? null,
                'capacity' => $data['capacity'] ?? 0,
                'status' => $data['status'] ?? 'Active',
                'id' => $data['id']
            ]);
        } else {
            $sql = "INSERT INTO batches (name, course_id, teacher_id, start_time, end_time, days, capacity, status) 
                    VALUES (:name, :course_id, :teacher_id, :start_time, :end_time, :days, :capacity, :status)";
            $stmt = $this->db->prepare($sql);
            return $stmt->execute([
                'name' => $data['name'],
                'course_id' => $data['course_id'],
                'teacher_id' => $data['teacher_id'],
                'start_time' => $data['start_time'] ?? null,
                'end_time' => $data['end_time'] ?? null,
                'days' => $data['days'] ?? null,
                'capacity' => $data['capacity'] ?? 0,
                'status' => $data['status'] ?? 'Active'
            ]);
        }
    }

    public function delete($id) {
        $stmt = $this->db->prepare("DELETE FROM batches WHERE id = :id");
        return $stmt->execute(['id' => $id]);
    }
}
