<?php

namespace App\Models;

use App\Core\Model;
use PDO;

class StudyMaterial extends Model {
    public function search($params = []) {
        $sql = "SELECT sm.*, c.name as course_name, b.name as batch_name 
                FROM study_materials sm 
                LEFT JOIN courses c ON sm.course_id = c.id 
                LEFT JOIN batches b ON sm.batch_id = b.id 
                WHERE 1=1";
        $binds = [];

        if (!empty($params['search'])) {
            $sql .= " AND (sm.title LIKE :search OR sm.description LIKE :search)";
            $binds['search'] = '%' . $params['search'] . '%';
        }

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

        if (!empty($params['filter_type'])) {
            $sql .= " AND sm.type = :type";
            $binds['type'] = $params['filter_type'];
        }

        $sql .= " ORDER BY sm.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 sm.*, c.name as course_name, b.name as batch_name 
                                    FROM study_materials sm 
                                    LEFT JOIN courses c ON sm.course_id = c.id 
                                    LEFT JOIN batches b ON sm.batch_id = b.id 
                                    WHERE sm.id = :id LIMIT 1");
        $stmt->execute(['id' => $id]);
        return $stmt->fetch();
    }

    public function save($data) {
        if (!empty($data['id'])) {
            $sql = "UPDATE study_materials SET title = :title, description = :description, 
                    course_id = :course_id, batch_id = :batch_id, type = :type, 
                    file_path = :file_path, link_url = :link_url, is_active = :is_active 
                    WHERE id = :id";
            $stmt = $this->db->prepare($sql);
            return $stmt->execute([
                'title' => $data['title'],
                'description' => $data['description'] ?? null,
                'course_id' => $data['course_id'] ?? null,
                'batch_id' => $data['batch_id'] ?? null,
                'type' => $data['type'] ?? 'Assignment',
                'file_path' => $data['file_path'] ?? null,
                'link_url' => $data['link_url'] ?? null,
                'is_active' => $data['is_active'] ?? 1,
                'id' => $data['id']
            ]);
        } else {
            $sql = "INSERT INTO study_materials (title, description, course_id, batch_id, type, file_path, link_url, is_active) 
                    VALUES (:title, :description, :course_id, :batch_id, :type, :file_path, :link_url, :is_active)";
            $stmt = $this->db->prepare($sql);
            return $stmt->execute([
                'title' => $data['title'],
                'description' => $data['description'] ?? null,
                'course_id' => $data['course_id'] ?? null,
                'batch_id' => $data['batch_id'] ?? null,
                'type' => $data['type'] ?? 'Assignment',
                'file_path' => $data['file_path'] ?? null,
                'link_url' => $data['link_url'] ?? null,
                'is_active' => $data['is_active'] ?? 1
            ]);
        }
    }

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

    public function toggleStatus($id) {
        $stmt = $this->db->prepare("UPDATE study_materials SET is_active = NOT is_active WHERE id = :id");
        return $stmt->execute(['id' => $id]);
    }
}
