<?php

namespace App\Models;

use App\Core\Model;
use PDO;

class FeeHead extends Model {
    public function getAll($params = []) {
        $sql = "SELECT * FROM fee_heads WHERE 1=1";
        $binds = [];

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

        if (isset($params['is_active']) && $params['is_active'] !== '') {
            $sql .= " AND is_active = :is_active";
            $binds['is_active'] = $params['is_active'];
        }

        if (isset($params['is_mandatory']) && $params['is_mandatory'] !== '') {
            $sql .= " AND is_mandatory = :is_mandatory";
            $binds['is_mandatory'] = $params['is_mandatory'];
        }

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

    public function search($params = []) {
        // Map filter_status to is_active if present
        if (isset($params['filter_status'])) {
            $params['is_active'] = $params['filter_status'];
        }
        return $this->getAll($params);
    }

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

    public function save($data) {
        if (!empty($data['id'])) {
            $sql = "UPDATE fee_heads SET 
                    name = :name, 
                    description = :description, 
                    default_amount = :default_amount, 
                    frequency = :frequency, 
                    is_active = :is_active, 
                    is_mandatory = :is_mandatory 
                    WHERE id = :id";
            $stmt = $this->db->prepare($sql);
            return $stmt->execute([
                'name' => $data['name'],
                'description' => $data['description'] ?? null,
                'default_amount' => $data['default_amount'] ?? 0,
                'frequency' => $data['frequency'] ?? 'Monthly',
                'is_active' => $data['is_active'] ?? 1,
                'is_mandatory' => $data['is_mandatory'] ?? 0,
                'id' => $data['id']
            ]);
        } else {
            $sql = "INSERT INTO fee_heads (name, description, default_amount, frequency, is_active, is_mandatory) 
                    VALUES (:name, :description, :default_amount, :frequency, :is_active, :is_mandatory)";
            $stmt = $this->db->prepare($sql);
            return $stmt->execute([
                'name' => $data['name'],
                'description' => $data['description'] ?? null,
                'default_amount' => $data['default_amount'] ?? 0,
                'frequency' => $data['frequency'] ?? 'Monthly',
                'is_active' => $data['is_active'] ?? 1,
                'is_mandatory' => $data['is_mandatory'] ?? 0
            ]);
        }
    }

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

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