<?php
    include('../session_timeout.php');
    include "../../db/connection.php";

    $login_user_id = $_SESSION['skybots_user_id'];
    $login_role = $_SESSION['skybots_role'];

    $type = $_POST['type'];

    date_default_timezone_set("Asia/Kolkata");
    $currentdatetime = date("Y-m-d H:i:s");
    $currentdate = date("Y-m-d");
    $currenttime = date("H-i-s");

    if ($type == 'add_model') {
        $errors = [];

        $model_id       = trim($_POST['model_id'] ?? '');
        $model_type       = trim($_POST['model_type'] ?? '');
        $drone_type       = trim($_POST['drone_type'] ?? '');
        $brand       = trim($_POST['brand'] ?? '');

        if ($model_type === '') { $errors[] = 'model_type'; }
        if ($drone_type === '') { $errors[] = 'drone_type'; }
        if ($brand === '') { $errors[] = 'brand'; }

        if (!empty($errors)) {
            echo json_encode(['status' => 'error', 'errors' => $errors]);
            exit;
        }

        // --- Check duplicate ---
        $check_sql = "SELECT id 
                        FROM model 
                        WHERE deletion = 0 
                            AND model = ?";
        $stmt_check = $conn->prepare($check_sql);
        $stmt_check->bind_param("s", $model_type);
        $stmt_check->execute();
        $result_check = $stmt_check->get_result();

        if ($result_check->num_rows > 0) {
            echo json_encode(['status' => 'exists', 'message' => 'Already Available..!']);
            exit;
        }

        // --- Insert new record ---
        $insert_sql = "INSERT INTO model (model_id, model, drone_type, brand, added_by, added_time)
                        VALUES ('NA', ?, ?, ?, ?, ?)";
        $stmt_insert = $conn->prepare($insert_sql);
        $stmt_insert->bind_param(
            "sssss",
            $model_type,
            $drone_type,
            $brand,
            $login_user_id,
            $currentdatetime
        );

        if ($stmt_insert->execute()) {
            $last_id = $conn->insert_id;
            $model_id = 'model_' . $last_id;
            
            $update_sql = "UPDATE model 
                        SET model_id = ? 
                        WHERE id = ?";
            $stmt_update = $conn->prepare($update_sql);
            $stmt_update->bind_param("si",$model_id, $last_id);
            $stmt_update->execute();

            echo json_encode(['status' => 'success', 'code' => 11]);
        } else {
            echo json_encode(['status' => 'error', 'message' => $conn->error, 'code' => 00]);
        }

        $stmt_insert->close();
        $stmt_check->close();
    }
    else if ($type == 'edit_model') {
        $errors = [];

        $model_id       = trim($_POST['model_id'] ?? '');
        $model_type       = trim($_POST['model_type'] ?? '');
        $drone_type       = trim($_POST['drone_type'] ?? '');
        $brand       = trim($_POST['brand'] ?? '');
        
        if ($model_type === '') { $errors[] = 'model_type'; }
        if ($drone_type === '') { $errors[] = 'drone_type'; }
        if ($brand === '') { $errors[] = 'brand'; }

        if (!empty($errors)) {
            echo json_encode(['status' => 'error', 'errors' => $errors]);
            exit;
        }
            
        $update_sql = "UPDATE model 
                    SET model = ?, drone_type = ?, brand = ? 
                    WHERE model_id = ?";
        $stmt_update = $conn->prepare($update_sql);
        $stmt_update->bind_param("ssss", $model_type, $drone_type, $brand, $model_id);
        if ($stmt_update->execute()) {
            echo json_encode(['status' => 'success', 'code' => 11]);
        } else {
            echo json_encode(['status' => 'error', 'message' => $conn->error, 'code' => 00]);
        }
        
    }
    else if ($type == 'delete_model') {
        $id = $_POST['id'];
        $sql = "UPDATE model SET deleted_by = '$login_user_id', deleted_time = '$currentdatetime', deletion = 1 WHERE model_id = '$id'";
        if ($conn->query($sql) === TRUE) {
            echo "11";
        } else {
            echo $conn->error;
        }
    } 
?>