<?php

require_once __DIR__ . '/db/config.php';

function update_process_definition()
{
    $pdo = db();

    $json_definition = <<<'EOT'
{
    "start_node_id": "awaiting_call",
    "eligibility_rules": [
        {
            "type": "person_property_equals",
            "params": {
                "property": "role",
                "value": "guest"
            }
        }
    ],
    "nodes": {
        "awaiting_call": {
            "ui_hints": {
                "title": "Follow-up Call",
                "status": "active",
                "reason": "Awaiting follow-up call with the guest.",
                "next_step": "Log the outcome of the call.",
                "form_schema": [
                    { "name": "call_date", "label": "Call Date", "type": "datetime-local", "default": "now", "required": true },
                    {
                        "name": "outcome_status", 
                        "label": "Call Outcome", 
                        "type": "select",
                        "required": true,
                        "options": [
                            { "value": "", "label": "-- Select Outcome --" },
                            { "value": "no_answer", "label": "No Answer" },
                            { "value": "wants_to_join", "label": "Wants to Join" },
                            { "value": "declined", "label": "Declined" },
                            { "value": "call_later", "label": "Call Later" }
                        ]
                    },
                    { "name": "note", "label": "Notes", "type": "textarea" },
                    { "name": "next_contact_date", "label": "Next Contact Date", "type": "datetime-local", "condition": { "field": "outcome_status", "value": "call_later" }, "required": true }
                ]
            }
        },
        "outcome_router": { "ui_hints": { "status": "processing" } },
        "waiting_for_next_contact": {
            "ui_hints": {
                "title": "Waiting for Scheduled Call",
                "status": "paused",
                "reason": "Waiting until the scheduled date for the next call.",
                "next_step": "Resume contact on or after the scheduled date."
            }
        },
        "decide_after_no_answer": {
            "ui_hints": {
                "title": "No Answer",
                "status": "paused",
                "reason": "The guest did not answer the call.",
                "next_step": "Decide whether to try again or end the process."
            }
        },
        "end_positive": { "ui_hints": { "title": "Wants to Join", "status": "completed", "reason": "Guest wants to join. New member process started.", "next_step": "" } },
        "end_negative_declined": { "ui_hints": { "title": "Declined", "status": "terminated", "reason": "Guest declined to join.", "next_step": "" } },
        "end_negative_no_answer": { "ui_hints": { "title": "Process Ended", "status": "terminated", "reason": "Process ended after no answer.", "next_step": "" } },
        "end_negative_terminated": { "ui_hints": { "title": "Process Terminated", "status": "terminated", "reason": "Process manually terminated by user.", "next_step": "" } }
    },
    "transitions": [
        {
            "id": "submit_outcome",
            "from": "awaiting_call",
            "to": "outcome_router",
            "name": "Submit Outcome",
            "actions": [ 
                { "type": "set_data", "params": { "keys": ["call_date", "outcome_status", "note", "next_contact_date"] } } 
            ] 
        },
        {
            "id": "route_wants_to_join",
            "from": "outcome_router",
            "to": "end_positive",
            "name": "Route to Positive End",
            "condition": { "field": "outcome_status", "value": "wants_to_join" },
            "actions": [
                { "type": "start_process", "process_code": "obsluga-przyjecia-nowego-czlonka" }
            ]
        },
        {
            "id": "route_declined",
            "from": "outcome_router",
            "to": "end_negative_declined",
            "name": "Route to Declined",
            "condition": { "field": "outcome_status", "value": "declined" }
        },
        {
            "id": "route_no_answer",
            "from": "outcome_router",
            "to": "decide_after_no_answer",
            "name": "Route to No Answer",
            "condition": { "field": "outcome_status", "value": "no_answer" }
        },
        {
            "id": "route_call_later",
            "from": "outcome_router",
            "to": "waiting_for_next_contact",
            "name": "Route to Call Later",
            "condition": { "field": "outcome_status", "value": "call_later" }
        },
        { "id": "continue_attempts", "from": "decide_after_no_answer", "to": "awaiting_call", "name": "Try Again" },
        { "id": "end_attempts", "from": "decide_after_no_answer", "to": "end_negative_no_answer", "name": "End Process" },
        { "id": "resume_contact", "from": "waiting_for_next_contact", "to": "awaiting_call", "name": "Resume / Attempt Call" },
        {
            "id": "terminate_from_awaiting_call",
            "from": "awaiting_call",
            "to": "end_negative_terminated",
            "name": "Zakończ proces",
            "actions": [ { "type": "set_data", "params": { "keys": ["termination_note"] } } ],
            "form_schema": [ { "name": "termination_note", "label": "Reason for Termination (optional)", "type": "textarea" } ]
        },
        {
            "id": "terminate_from_decide",
            "from": "decide_after_no_answer",
            "to": "end_negative_terminated",
            "name": "Zakończ proces",
            "actions": [ { "type": "set_data", "params": { "keys": ["termination_note"] } } ],
            "form_schema": [ { "name": "termination_note", "label": "Reason for Termination (optional)", "type": "textarea" } ]
        },
        {
            "id": "terminate_from_waiting",
            "from": "waiting_for_next_contact",
            "to": "end_negative_terminated",
            "name": "Zakończ proces",
            "actions": [ { "type": "set_data", "params": { "keys": ["termination_note"] } } ],
            "form_schema": [ { "name": "termination_note", "label": "Reason for Termination (optional)", "type": "textarea" } ]
        }
    ]
}
EOT;

    // Update process_definition_id=4
    $stmt = $pdo->prepare("UPDATE process_definitions SET definition_json = :json, start_node_id = 'awaiting_call' WHERE id = 4");
    $stmt->execute([
        ':json' => $json_definition,
    ]);

    echo "Updated process_definition_id=4 with the new follow-up workflow.\n";

    // Deactivate process_definition_id=3
    $stmt = $pdo->prepare("UPDATE process_definitions SET is_active = 0 WHERE id = 3");
    $stmt->execute();
    
    echo "Deactivated process_definition_id=3 ('guest_handling').\n";
}

// Direct execution guard
if (basename(__FILE__) == basename($_SERVER["SCRIPT_FILENAME"]))
{
    try {
        update_process_definition();
    } catch (Exception $e) {
        echo "Failed to update process definitions: " . $e->getMessage() . "\n";
    }
}
