<?php
header("Content-Type: application/json");
require("../../includes/config.php");

$response = array('status' => '', 'message' => '');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $fee_title = trim($_POST['fee_title'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $amount = trim($_POST['amount'] ?? '');
    $class_id = trim($_POST['class_id'] ?? '');
    $academic_year_id = trim($_POST['academic_year_id'] ?? '');
    
    // Captured km_range and medium_id (Stream removed)
    $km_range = trim($_POST['km_range'] ?? 0);
    $medium_id = trim($_POST['medium_id'] ?? '');

    // Convert empty values to NULL for database compatibility
    $medium_val = ($medium_id !== '') ? $medium_id : null;

    // Validation: Stream removed from requirements
    if (empty($fee_title) || empty($amount) || empty($class_id) || empty($academic_year_id) || empty($medium_id)) {
        $response['status'] = 'error';
        $response['message'] = 'Fee Title, Class, Medium, Academic Year, and Amount are required.';
    } else {
        try {
            // 1. Check for duplicate fee_type
            // Removed stream_id from the uniqueness check
            $checkSql = "SELECT COUNT(*) FROM fee_types 
                         WHERE fee_title = :fee_title 
                         AND class_id = :class_id 
                         AND academic_year_id = :year_id 
                         AND km_range = :km_range
                         AND medium_id = :medium_id";
            
            $checkStmt = $pdo->prepare($checkSql);
            $checkStmt->execute([
                ':fee_title' => $fee_title, 
                ':class_id'  => $class_id,
                ':year_id'   => $academic_year_id,
                ':km_range'  => $km_range,
                ':medium_id' => $medium_val
            ]);
            
            if ($checkStmt->fetchColumn() > 0) {
                $response['status'] = 'error';
                $response['message'] = "This fee type already exists for this specific Class/Medium/KM configuration.";
            } else {
                // 2. INSERT: Removed stream_id column and parameter
                $sql = "INSERT INTO fee_types (fee_title, class_id, medium_id, academic_year_id, description, amount, km_range) 
                        VALUES (:fee_title, :class_id, :medium_id, :year_id, :description, :amount, :km_range)";
                
                $stmt = $pdo->prepare($sql);
                
                $params = [
                    ':fee_title'   => $fee_title,
                    ':class_id'    => $class_id,
                    ':medium_id'   => $medium_val,
                    ':year_id'     => $academic_year_id,
                    ':description' => $description,
                    ':amount'      => $amount,
                    ':km_range'    => $km_range
                ];

                if ($stmt->execute($params)) {
                    $response['status'] = 'success';
                    $response['message'] = 'Fee Type added successfully.';
                } else {
                    $response['status'] = 'error';
                    $response['message'] = 'Failed to add fee type.';
                }
            }
        } catch (PDOException $e) {
            $response['status'] = 'error';
            $response['message'] = 'Database Error: ' . $e->getMessage();
        }
    }
}

echo json_encode($response);
?>