<?php

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

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

    $process_code = 'guest_handling';

    $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" },
                    { "name": "note", "label": "Notes", "type": "textarea" }
                ]
            }
        },
        "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": ""
            }
        }
    },
    "transitions": [
        {
            "id": "log_wants_to_join",
            "from": "awaiting_call",
            "to": "end_positive",
            "name": "Wants to Join",
            "actions": [
                {
                    "type": "set_data",
                    "params": { "keys": ["call_date", "note", "outcome_status"] }
                },
                {
                    "type": "start_process",
                    "process_code": "obsluga-przyjecia-nowego-czlonka"
                }
            ]
        },
        {
            "id": "log_declined",
            "from": "awaiting_call",
            "to": "end_negative_declined",
            "name": "Declined",
            "actions": [
                {
                    "type": "set_data",
                    "params": { "keys": ["call_date", "note", "outcome_status"] }
                }
            ]
        },
        {
            "id": "log_no_answer",
            "from": "awaiting_call",
            "to": "decide_after_no_answer",
            "name": "No Answer",
            "actions": [
                {
                    "type": "set_data",
                    "params": { "keys": ["call_date", "note", "outcome_status"] }
                }
            ]
        },
        {
            "id": "log_call_later",
            "from": "awaiting_call",
            "to": "awaiting_call",
            "name": "Call Later",
            "actions": [
                {
                    "type": "set_data",
                    "params": { "keys": ["call_date", "note", "outcome_status"] }
                }
            ]
        },
        {
            "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"
        }
    ]
}
EOT;

    $stmt = $pdo->prepare("UPDATE process_definitions SET definition_json = :json, start_node_id = 'awaiting_call' WHERE code = :code");
    $stmt->execute([
        ':json' => $json_definition,
        ':code' => $process_code,
    ]);

    echo "Migration 024 executed successfully: Updated 'guest_handling' process definition.\n";
}

// Direct execution guard
if (basename(__FILE__) == basename($_SERVER["SCRIPT_FILENAME"])) {
    try {
        migrate_024();
    } catch (Exception $e) {
        echo "Migration 24 failed: " . $e->getMessage() . "\n";
    }
}
