<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    exit(0);
}

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../models/Subproject.php';

$database = new Database();
$db = $database->getConnection();

$subproject = new Subproject($db);

$request_method = $_SERVER['REQUEST_METHOD'];

switch($request_method) {
    case 'GET':
        if(isset($_GET['id'])) {
            $subproject->id = $_GET['id'];
            $subproject->readOne();
            
            if($subproject->subproject_name != null) {
                $subproject_arr = array(
                    "id" => $subproject->id,
                    "project_id" => $subproject->project_id,
                    "subproject_name" => $subproject->subproject_name,
                    "description" => $subproject->description,
                    "status" => $subproject->status,
                    "start_date" => $subproject->start_date,
                    "end_date" => $subproject->end_date,
                    "budget" => $subproject->budget,
                    "created_at" => $subproject->created_at,
                    "updated_at" => $subproject->updated_at,
                    "status_badge" => $subproject->getStatusBadge(),
                    "progress" => $subproject->getProgress()
                );
                
                http_response_code(200);
                echo json_encode($subproject_arr);
            } else {
                http_response_code(404);
                echo json_encode(array("message" => "Subproject not found."));
            }
        } elseif(isset($_GET['project_id'])) {
            $stmt = $subproject->read();
            $num = $stmt->rowCount();
            
            if($num > 0) {
                $subprojects_arr = array();
                $subprojects_arr["records"] = array();
                
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    extract($row);
                    
                    $subproject_item = array(
                        "id" => $id,
                        "project_id" => $project_id,
                        "subproject_name" => $subproject_name,
                        "description" => $description,
                        "status" => $status,
                        "start_date" => $start_date,
                        "end_date" => $end_date,
                        "budget" => $budget,
                        "created_at" => $created_at,
                        "updated_at" => $updated_at,
                        "status_badge" => $subproject->getStatusBadge(),
                        "progress" => $subproject->getProgress()
                    );
                    
                    array_push($subprojects_arr["records"], $subproject_item);
                }
                
                http_response_code(200);
                echo json_encode($subprojects_arr);
            } else {
                http_response_code(200);
                echo json_encode(array("records" => array()));
            }
        } else {
            $stmt = $subproject->read();
            $num = $stmt->rowCount();
            
            if($num > 0) {
                $subprojects_arr = array();
                $subprojects_arr["records"] = array();
                
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    extract($row);
                    
                    $subproject_item = array(
                        "id" => $id,
                        "project_id" => $project_id,
                        "subproject_name" => $subproject_name,
                        "description" => $description,
                        "status" => $status,
                        "start_date" => $start_date,
                        "end_date" => $end_date,
                        "budget" => $budget,
                        "created_at" => $created_at,
                        "updated_at" => $updated_at,
                        "status_badge" => $subproject->getStatusBadge(),
                        "progress" => $subproject->getProgress()
                    );
                    
                    array_push($subprojects_arr["records"], $subproject_item);
                }
                
                http_response_code(200);
                echo json_encode($subprojects_arr);
            } else {
                http_response_code(200);
                echo json_encode(array("records" => array()));
            }
        }
        break;
        
    case 'POST':
        $data = json_decode(file_get_contents("php://input"));
        
        if(!empty($data->project_id) && !empty($data->subproject_name)) {
            $subproject->project_id = $data->project_id;
            $subproject->subproject_name = $data->subproject_name;
            $subproject->description = $data->description ?? '';
            $subproject->status = $data->status ?? 'planning';
            $subproject->start_date = $data->start_date ?? null;
            $subproject->end_date = $data->end_date ?? null;
            $subproject->budget = $data->budget ?? null;
            
            if($subproject->create()) {
                http_response_code(201);
                echo json_encode(array("message" => "Subproject was created."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to create subproject."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to create subproject. Project ID and subproject name are required."));
        }
        break;
        
    case 'PUT':
        $data = json_decode(file_get_contents("php://input"));
        
        if(!empty($data->id) && !empty($data->project_id) && !empty($data->subproject_name)) {
            $subproject->id = $data->id;
            $subproject->project_id = $data->project_id;
            $subproject->subproject_name = $data->subproject_name;
            $subproject->description = $data->description ?? '';
            $subproject->status = $data->status ?? 'planning';
            $subproject->start_date = $data->start_date ?? null;
            $subproject->end_date = $data->end_date ?? null;
            $subproject->budget = $data->budget ?? null;
            
            if($subproject->update()) {
                http_response_code(200);
                echo json_encode(array("message" => "Subproject was updated."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to update subproject."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to update subproject. Data is incomplete."));
        }
        break;
        
    case 'DELETE':
        if(isset($_GET['id'])) {
            $subproject->id = $_GET['id'];
            
            if($subproject->delete()) {
                http_response_code(200);
                echo json_encode(array("message" => "Subproject was deleted."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to delete subproject."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to delete subproject. ID is missing."));
        }
        break;
        
    default:
        http_response_code(405);
        echo json_encode(array("message" => "Method not allowed."));
        break;
}
?>
