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

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

$contactPerson = new ContactPerson($db);

$request_method = $_SERVER['REQUEST_METHOD'];

switch($request_method) {
    case 'GET':
        if(isset($_GET['id'])) {
            $contactPerson->id = $_GET['id'];
            $contactPerson->readOne();
            
            if($contactPerson->first_name != null) {
                $contact_arr = array(
                    "id" => $contactPerson->id,
                    "client_id" => $contactPerson->client_id,
                    "first_name" => $contactPerson->first_name,
                    "last_name" => $contactPerson->last_name,
                    "email" => $contactPerson->email,
                    "phone" => $contactPerson->phone,
                    "position" => $contactPerson->position,
                    "department" => $contactPerson->department,
                    "is_primary" => $contactPerson->is_primary,
                    "notes" => $contactPerson->notes,
                    "created_at" => $contactPerson->created_at,
                    "updated_at" => $contactPerson->updated_at
                );
                
                http_response_code(200);
                echo json_encode($contact_arr);
            } else {
                http_response_code(404);
                echo json_encode(array("message" => "Contact person not found."));
            }
        } elseif(isset($_GET['client_id'])) {
            $contactPerson->client_id = $_GET['client_id'];
            $stmt = $contactPerson->read();
            $num = $stmt->rowCount();
            
            if($num > 0) {
                $contacts_arr = array();
                $contacts_arr["records"] = array();
                
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    extract($row);
                    
                    $contact_item = array(
                        "id" => $id,
                        "client_id" => $client_id,
                        "first_name" => $first_name,
                        "last_name" => $last_name,
                        "email" => $email,
                        "phone" => $phone,
                        "position" => $position,
                        "department" => $department,
                        "is_primary" => $is_primary,
                        "notes" => $notes,
                        "created_at" => $created_at,
                        "updated_at" => $updated_at
                    );
                    
                    array_push($contacts_arr["records"], $contact_item);
                }
                
                http_response_code(200);
                echo json_encode($contacts_arr);
            } else {
                http_response_code(200);
                echo json_encode(array("records" => array()));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Missing client_id parameter."));
        }
        break;
        
    case 'POST':
        $data = json_decode(file_get_contents("php://input"));
        
        if(!empty($data->client_id) && !empty($data->first_name) && !empty($data->last_name)) {
            $contactPerson->client_id = $data->client_id;
            $contactPerson->first_name = $data->first_name;
            $contactPerson->last_name = $data->last_name;
            $contactPerson->email = $data->email ?? '';
            $contactPerson->phone = $data->phone ?? '';
            $contactPerson->position = $data->position ?? '';
            $contactPerson->department = $data->department ?? '';
            $contactPerson->notes = $data->notes ?? '';
            $contactPerson->is_primary = $data->is_primary ?? false;
            
            if($contactPerson->create()) {
                http_response_code(201);
                echo json_encode(array("message" => "Contact person was created."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to create contact person."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to create contact person. Data is incomplete."));
        }
        break;
        
    case 'PUT':
        $data = json_decode(file_get_contents("php://input"));
        
        if(!empty($data->id) && !empty($data->client_id) && !empty($data->first_name) && !empty($data->last_name)) {
            $contactPerson->id = $data->id;
            $contactPerson->client_id = $data->client_id;
            $contactPerson->first_name = $data->first_name;
            $contactPerson->last_name = $data->last_name;
            $contactPerson->email = $data->email ?? '';
            $contactPerson->phone = $data->phone ?? '';
            $contactPerson->position = $data->position ?? '';
            $contactPerson->department = $data->department ?? '';
            $contactPerson->notes = $data->notes ?? '';
            $contactPerson->is_primary = $data->is_primary ?? false;
            
            if($contactPerson->update()) {
                http_response_code(200);
                echo json_encode(array("message" => "Contact person was updated."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to update contact person."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to update contact person. Data is incomplete."));
        }
        break;
        
    case 'DELETE':
        if(isset($_GET['id'])) {
            $contactPerson->id = $_GET['id'];
            
            if($contactPerson->delete()) {
                http_response_code(200);
                echo json_encode(array("message" => "Contact person was deleted."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to delete contact person."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to delete contact person. ID is missing."));
        }
        break;
        
    default:
        http_response_code(405);
        echo json_encode(array("message" => "Method not allowed."));
        break;
}
?>
