<?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/Payment.php';

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

$payment = new Payment($db);

$request_method = $_SERVER['REQUEST_METHOD'];

switch($request_method) {
    case 'GET':
        if(isset($_GET['id'])) {
            $payment->id = $_GET['id'];
            $payment->readOne();
            
            if($payment->payment_date != null) {
                $payment_arr = array(
                    "id" => $payment->id,
                    "invoice_id" => $payment->invoice_id,
                    "client_id" => $payment->client_id,
                    "payment_date" => $payment->payment_date,
                    "amount" => $payment->amount,
                    "payment_method" => $payment->payment_method,
                    "reference_number" => $payment->reference_number,
                    "notes" => $payment->notes,
                    "created_at" => $payment->created_at,
                    "updated_at" => $payment->updated_at
                );
                
                http_response_code(200);
                echo json_encode($payment_arr);
            } else {
                http_response_code(404);
                echo json_encode(array("message" => "Payment not found."));
            }
        } elseif(isset($_GET['invoice_id'])) {
            $payment->invoice_id = $_GET['invoice_id'];
            $stmt = $payment->read();
            $num = $stmt->rowCount();
            
            if($num > 0) {
                $payments_arr = array();
                $payments_arr["records"] = array();
                
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    extract($row);
                    
                    $payment_item = array(
                        "id" => $id,
                        "invoice_id" => $invoice_id,
                        "client_id" => $client_id,
                        "payment_date" => $payment_date,
                        "amount" => $amount,
                        "payment_method" => $payment_method,
                        "reference_number" => $reference_number,
                        "notes" => $notes,
                        "created_at" => $created_at,
                        "updated_at" => $updated_at
                    );
                    
                    array_push($payments_arr["records"], $payment_item);
                }
                
                http_response_code(200);
                echo json_encode($payments_arr);
            } else {
                http_response_code(200);
                echo json_encode(array("records" => array()));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Missing invoice_id parameter."));
        }
        break;
        
    case 'POST':
        $data = json_decode(file_get_contents("php://input"));
        
        if(!empty($data->client_id) && !empty($data->payment_date) && !empty($data->amount)) {
            $payment->client_id = $data->client_id;
            $payment->invoice_id = $data->invoice_id ?? null;
            $payment->payment_date = $data->payment_date;
            $payment->amount = $data->amount;
            $payment->payment_method = $data->payment_method ?? 'cash';
            $payment->reference_number = $data->reference_number ?? '';
            $payment->notes = $data->notes ?? '';
            
            if($payment->create()) {
                http_response_code(201);
                echo json_encode(array("message" => "Payment was created."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to create payment."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to create payment. Data is incomplete."));
        }
        break;
        
    case 'PUT':
        $data = json_decode(file_get_contents("php://input"));
        
        if(!empty($data->id) && !empty($data->client_id) && !empty($data->payment_date) && !empty($data->amount)) {
            $payment->id = $data->id;
            $payment->client_id = $data->client_id;
            $payment->invoice_id = $data->invoice_id ?? null;
            $payment->payment_date = $data->payment_date;
            $payment->amount = $data->amount;
            $payment->payment_method = $data->payment_method ?? 'cash';
            $payment->reference_number = $data->reference_number ?? '';
            $payment->notes = $data->notes ?? '';
            
            if($payment->update()) {
                http_response_code(200);
                echo json_encode(array("message" => "Payment was updated."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to update payment."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to update payment. Data is incomplete."));
        }
        break;
        
    case 'DELETE':
        if(isset($_GET['id'])) {
            $payment->id = $_GET['id'];
            
            if($payment->delete()) {
                http_response_code(200);
                echo json_encode(array("message" => "Payment was deleted."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to delete payment."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to delete payment. ID is missing."));
        }
        break;
        
    default:
        http_response_code(405);
        echo json_encode(array("message" => "Method not allowed."));
        break;
}
?>
