<?php

/**
 * AJAX route definitions.
 *
 * All routes in the middleware group require a valid nonce and admin privileges.
 * The $router variable is provided by HookProvider before this file is included.
 */

use EasyLinkAutomations\HTTP\Router\Route;
use EasyLinkAutomations\HTTP\Controllers\AutomationController;
use EasyLinkAutomations\HTTP\Controllers\ConnectionController;
use EasyLinkAutomations\HTTP\Controllers\DashboardController;
use EasyLinkAutomations\HTTP\Controllers\LogController;
use EasyLinkAutomations\HTTP\Controllers\SettingsController;

if (!defined('ABSPATH')) {
    exit;
}

if (!headers_sent()) {
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');

    $easylink_automations_request_method = isset($_SERVER['REQUEST_METHOD']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_METHOD'])) : '';
    if ($easylink_automations_request_method === 'OPTIONS') {
        status_header(200);
        exit;
    }
}

// ──────────────────────────────────────────────
//  Protected routes (nonce + admin check)
//  NOTE: All routes use POST because WordPress admin-ajax.php
//        processes requests via POST with FormData.
// ──────────────────────────────────────────────

Route::group(function (): void {
    // Dashboard
    Route::post('dashboard', [DashboardController::class, 'index']);

    // Automations - static routes first, then dynamic
    Route::post('automations', [AutomationController::class, 'index']);
    Route::post('automations/save', [AutomationController::class, 'store']);
    Route::post('automations/update', [AutomationController::class, 'update']);
    Route::post('automations/toggle', [AutomationController::class, 'toggleStatus']);
    Route::post('automations/delete', [AutomationController::class, 'destroy']);
    Route::post('automations/duplicate', [AutomationController::class, 'duplicate']);
    Route::post('automations/show', [AutomationController::class, 'show']);

    // Connections
    Route::post('connections', [ConnectionController::class, 'index']);
    Route::post('connections/save', [ConnectionController::class, 'store']);
    Route::post('connections/platforms', [ConnectionController::class, 'platforms']);
    Route::post('connections/test', [ConnectionController::class, 'test']);
    Route::post('connections/update', [ConnectionController::class, 'update']);
    Route::post('connections/delete', [ConnectionController::class, 'destroy']);
    Route::post('connections/oauth/start', [ConnectionController::class, 'oauthStart']);

    // Logs
    Route::post('logs', [LogController::class, 'index']);
    Route::post('logs/show', [LogController::class, 'show']);
    Route::post('logs/delete', [LogController::class, 'destroy']);
    Route::post('logs/clear', [LogController::class, 'clear']);

    // Settings
    Route::post('settings', [SettingsController::class, 'getSettings']);
    Route::post('settings/update', [SettingsController::class, 'updateSettings']);

    // Triggers & Actions catalog (authenticated)
    Route::post('triggers', [\EasyLinkAutomations\HTTP\Controllers\TriggerController::class, 'index']);
    Route::post('triggers/sample-fields', [\EasyLinkAutomations\HTTP\Controllers\TriggerController::class, 'sampleFields']);
    Route::post('triggers/{slug}', [\EasyLinkAutomations\HTTP\Controllers\TriggerController::class, 'show']);
    Route::post('actions', [\EasyLinkAutomations\HTTP\Controllers\ActionController::class, 'index']);
    Route::post('actions/{slug}', [\EasyLinkAutomations\HTTP\Controllers\ActionController::class, 'show']);

    // ─── Trigger-specific API controllers ────────────────────
    Route::post('forminator/form-fields',       [\EasyLinkAutomations\HTTP\Controllers\Triggers\ForminatorController::class, 'formFields']);
    Route::post('wpforms/form-fields',          [\EasyLinkAutomations\HTTP\Controllers\Triggers\WPFormsController::class, 'formFields']);
    Route::post('gravityforms/form-fields',     [\EasyLinkAutomations\HTTP\Controllers\Triggers\GravityFormsController::class, 'formFields']);
    Route::post('fluent-form/form-fields',      [\EasyLinkAutomations\HTTP\Controllers\Triggers\FluentFormController::class, 'formFields']);
    Route::post('contact-form7/form-fields',    [\EasyLinkAutomations\HTTP\Controllers\Triggers\ContactForm7Controller::class, 'formFields']);
    Route::post('formidable-forms/form-fields', [\EasyLinkAutomations\HTTP\Controllers\Triggers\FormidableFormsController::class, 'formFields']);
    Route::post('jetformbuilder/form-fields',   [\EasyLinkAutomations\HTTP\Controllers\Triggers\JetFormBuilderController::class, 'formFields']);
    Route::post('happyforms/form-fields',       [\EasyLinkAutomations\HTTP\Controllers\Triggers\HappyFormsController::class, 'formFields']);
    Route::post('mailpoet/form-fields',         [\EasyLinkAutomations\HTTP\Controllers\Triggers\MailPoetController::class, 'formFields']);
    Route::post('ninja-forms/form-fields',     [\EasyLinkAutomations\HTTP\Controllers\Triggers\NinjaFormsController::class, 'formFields']);
    Route::post('metform/form-fields',         [\EasyLinkAutomations\HTTP\Controllers\Triggers\MetFormController::class, 'formFields']);
    Route::post('sureforms/form-fields',       [\EasyLinkAutomations\HTTP\Controllers\Triggers\SureFormsController::class, 'formFields']);
    Route::post('givewp/form-fields',          [\EasyLinkAutomations\HTTP\Controllers\Triggers\GiveWPController::class, 'formFields']);
    Route::post('paymattic/form-fields',       [\EasyLinkAutomations\HTTP\Controllers\Triggers\PaymatticController::class, 'formFields']);
    Route::post('convertpro/form-fields',      [\EasyLinkAutomations\HTTP\Controllers\Triggers\ConvertProController::class, 'formFields']);
    Route::post('pie-forms/form-fields',       [\EasyLinkAutomations\HTTP\Controllers\Triggers\PieFormsController::class, 'formFields']);
    Route::post('ws-form/form-fields',         [\EasyLinkAutomations\HTTP\Controllers\Triggers\WSFormController::class, 'formFields']);
    Route::post('elementor-pro/form-fields',   [\EasyLinkAutomations\HTTP\Controllers\Triggers\ElementorProController::class, 'formFields']);
    Route::post('divi-builder/form-fields',    [\EasyLinkAutomations\HTTP\Controllers\Triggers\DiviBuilderController::class, 'formFields']);
    Route::post('bricks-builder/form-fields',  [\EasyLinkAutomations\HTTP\Controllers\Triggers\BricksBuilderController::class, 'formFields']);
    Route::post('kadence-forms/form-fields',   [\EasyLinkAutomations\HTTP\Controllers\Triggers\KadenceFormsController::class, 'formFields']);
    Route::post('ultimate-addons-for-gutenberg/form-fields', [\EasyLinkAutomations\HTTP\Controllers\Triggers\UAGController::class, 'formFields']);
    Route::post('beaver-builder/form-fields',  [\EasyLinkAutomations\HTTP\Controllers\Triggers\BeaverBuilderController::class, 'formFields']);
    Route::post('thrive-leads/form-fields',    [\EasyLinkAutomations\HTTP\Controllers\Triggers\ThriveLeadsController::class, 'formFields']);
    Route::post('newsletter/form-fields',      [\EasyLinkAutomations\HTTP\Controllers\Triggers\NewsletterController::class, 'formFields']);
    Route::post('metabox/form-fields',         [\EasyLinkAutomations\HTTP\Controllers\Triggers\MetaBoxController::class, 'formFields']);

    // ─── Action-specific API controllers ───────────────────────
    // Microsoft Teams
    Route::post('microsoft-teams/teams', [\EasyLinkAutomations\HTTP\Controllers\Actions\MicrosoftTeamsController::class, 'teams']);
    Route::post('microsoft-teams/channels', [\EasyLinkAutomations\HTTP\Controllers\Actions\MicrosoftTeamsController::class, 'channels']);
    Route::post('microsoft-teams/chats', [\EasyLinkAutomations\HTTP\Controllers\Actions\MicrosoftTeamsController::class, 'chats']);
    Route::post('microsoft-teams/plans', [\EasyLinkAutomations\HTTP\Controllers\Actions\MicrosoftTeamsController::class, 'plans']);
    Route::post('microsoft-teams/buckets', [\EasyLinkAutomations\HTTP\Controllers\Actions\MicrosoftTeamsController::class, 'buckets']);
    Route::post('microsoft-teams/members', [\EasyLinkAutomations\HTTP\Controllers\Actions\MicrosoftTeamsController::class, 'members']);

    // Google Sheets
    Route::post('google-sheets/spreadsheets', [\EasyLinkAutomations\HTTP\Controllers\Actions\GoogleSheetsController::class, 'spreadsheets']);
    Route::post('google-sheets/sheets', [\EasyLinkAutomations\HTTP\Controllers\Actions\GoogleSheetsController::class, 'sheets']);
    Route::post('google-sheets/columns', [\EasyLinkAutomations\HTTP\Controllers\Actions\GoogleSheetsController::class, 'columns']);

    // Mailchimp
    Route::post('mailchimp/lists', [\EasyLinkAutomations\HTTP\Controllers\Actions\MailchimpController::class, 'lists']);
    Route::post('mailchimp/merge-fields', [\EasyLinkAutomations\HTTP\Controllers\Actions\MailchimpController::class, 'mergeFields']);
    Route::post('mailchimp/campaigns', [\EasyLinkAutomations\HTTP\Controllers\Actions\MailchimpController::class, 'campaigns']);

    // ActiveCampaign
    Route::post('active-campaign/tags', [\EasyLinkAutomations\HTTP\Controllers\Actions\ActiveCampaignController::class, 'tags']);
    Route::post('active-campaign/lists', [\EasyLinkAutomations\HTTP\Controllers\Actions\ActiveCampaignController::class, 'lists']);
    Route::post('active-campaign/contacts', [\EasyLinkAutomations\HTTP\Controllers\Actions\ActiveCampaignController::class, 'contacts']);
    Route::post('active-campaign/contact-fields', [\EasyLinkAutomations\HTTP\Controllers\Actions\ActiveCampaignController::class, 'contactFields']);
    Route::post('active-campaign/account-fields', [\EasyLinkAutomations\HTTP\Controllers\Actions\ActiveCampaignController::class, 'accountFields']);
    Route::post('active-campaign/accounts', [\EasyLinkAutomations\HTTP\Controllers\Actions\ActiveCampaignController::class, 'accounts']);
    Route::post('active-campaign/deals', [\EasyLinkAutomations\HTTP\Controllers\Actions\ActiveCampaignController::class, 'deals']);

    // Brevo
    Route::post('brevo/contact-attributes', [\EasyLinkAutomations\HTTP\Controllers\Actions\BrevoController::class, 'contactAttributes']);
    Route::post('brevo/lists', [\EasyLinkAutomations\HTTP\Controllers\Actions\BrevoController::class, 'lists']);
    Route::post('brevo/templates', [\EasyLinkAutomations\HTTP\Controllers\Actions\BrevoController::class, 'templates']);
    Route::post('brevo/senders', [\EasyLinkAutomations\HTTP\Controllers\Actions\BrevoController::class, 'senders']);
    Route::post('brevo/sender-ids', [\EasyLinkAutomations\HTTP\Controllers\Actions\BrevoController::class, 'senderIds']);

    // CleverReach
    Route::post('cleverreach/groups', [\EasyLinkAutomations\HTTP\Controllers\Actions\CleverReachController::class, 'groups']);
    Route::post('cleverreach/receiver-fields', [\EasyLinkAutomations\HTTP\Controllers\Actions\CleverReachController::class, 'receiverFields']);
    Route::post('cleverreach/mailings', [\EasyLinkAutomations\HTTP\Controllers\Actions\CleverReachController::class, 'mailings']);
    Route::post('cleverreach/reports', [\EasyLinkAutomations\HTTP\Controllers\Actions\CleverReachController::class, 'reports']);

    // rapidmail
    Route::post('rapidmail/recipientlists', [\EasyLinkAutomations\HTTP\Controllers\Actions\RapidmailController::class, 'recipientlists']);
    Route::post('rapidmail/recipient-fields', [\EasyLinkAutomations\HTTP\Controllers\Actions\RapidmailController::class, 'recipientFields']);
    Route::post('rapidmail/mailings', [\EasyLinkAutomations\HTTP\Controllers\Actions\RapidmailController::class, 'mailings']);
    Route::post('rapidmail/mailing-folders', [\EasyLinkAutomations\HTTP\Controllers\Actions\RapidmailController::class, 'mailingFolders']);

    // MailerLite
    Route::post('mailerlite/fields', [\EasyLinkAutomations\HTTP\Controllers\Actions\MailerLiteController::class, 'fields']);
    Route::post('mailerlite/update-fields', [\EasyLinkAutomations\HTTP\Controllers\Actions\MailerLiteController::class, 'updateFields']);

    // Notion
    Route::post('notion/databases', [\EasyLinkAutomations\HTTP\Controllers\Actions\NotionController::class, 'databases']);
    Route::post('notion/database-properties', [\EasyLinkAutomations\HTTP\Controllers\Actions\NotionController::class, 'databaseProperties']);

    // Telegram
    Route::post('telegram/chats', [\EasyLinkAutomations\HTTP\Controllers\Actions\TelegramController::class, 'chats']);

    // Slack
    Route::post('slack/channels', [\EasyLinkAutomations\HTTP\Controllers\Actions\SlackController::class, 'channels']);
    Route::post('slack/users', [\EasyLinkAutomations\HTTP\Controllers\Actions\SlackController::class, 'users']);
    Route::post('slack/profile-fields', [\EasyLinkAutomations\HTTP\Controllers\Actions\SlackController::class, 'profileFields']);
    Route::post('slack/usergroups', [\EasyLinkAutomations\HTTP\Controllers\Actions\SlackController::class, 'usergroups']);

    // WordPress
    Route::post('wordpress/post-fields', [\EasyLinkAutomations\HTTP\Controllers\Actions\WordPressController::class, 'postFields']);
    Route::post('wordpress/user-meta-fields', [\EasyLinkAutomations\HTTP\Controllers\Actions\WordPressController::class, 'userMetaFields']);
    Route::post('wordpress/post-types', [\EasyLinkAutomations\HTTP\Controllers\Actions\WordPressController::class, 'postTypes']);
    Route::post('wordpress/users', [\EasyLinkAutomations\HTTP\Controllers\Actions\WordPressController::class, 'users']);

    // Trello
    Route::post('trello/boards',   [\EasyLinkAutomations\HTTP\Controllers\Actions\TrelloController::class, 'boards']);
    Route::post('trello/lists',    [\EasyLinkAutomations\HTTP\Controllers\Actions\TrelloController::class, 'lists']);
    Route::post('trello/cards',    [\EasyLinkAutomations\HTTP\Controllers\Actions\TrelloController::class, 'cards']);
    Route::post('trello/members',  [\EasyLinkAutomations\HTTP\Controllers\Actions\TrelloController::class, 'members']);
    Route::post('trello/labels',   [\EasyLinkAutomations\HTTP\Controllers\Actions\TrelloController::class, 'labels']);

    // Sendy
    Route::post('sendy/brands', [\EasyLinkAutomations\HTTP\Controllers\Actions\SendyController::class, 'brands']);
    Route::post('sendy/lists',  [\EasyLinkAutomations\HTTP\Controllers\Actions\SendyController::class, 'lists']);

    // Discord
    Route::post('discord/guilds',        [\EasyLinkAutomations\HTTP\Controllers\Actions\DiscordController::class, 'guilds']);
    Route::post('discord/channels',      [\EasyLinkAutomations\HTTP\Controllers\Actions\DiscordController::class, 'channels']);
    Route::post('discord/text-channels', [\EasyLinkAutomations\HTTP\Controllers\Actions\DiscordController::class, 'textChannels']);
    Route::post('discord/roles',         [\EasyLinkAutomations\HTTP\Controllers\Actions\DiscordController::class, 'roles']);

    // Freshservice
    Route::post('freshservice/agents',           [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'agents']);
    Route::post('freshservice/agent-groups',     [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'agentGroups']);
    Route::post('freshservice/agent-roles',      [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'agentRoles']);
    Route::post('freshservice/announcements',    [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'announcements']);
    Route::post('freshservice/asset-types',      [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'assetTypes']);
    Route::post('freshservice/changes',          [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'changes']);
    Route::post('freshservice/departments',      [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'departments']);
    Route::post('freshservice/locations',        [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'locations']);
    Route::post('freshservice/problems',         [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'problems']);
    Route::post('freshservice/products',         [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'products']);
    Route::post('freshservice/releases',         [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'releases']);
    Route::post('freshservice/requesters',       [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'requesters']);
    Route::post('freshservice/requester-groups', [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'requesterGroups']);
    Route::post('freshservice/software',         [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'software']);
    Route::post('freshservice/tickets',          [\EasyLinkAutomations\HTTP\Controllers\Actions\FreshserviceController::class, 'tickets']);
})->middleware('nonce', 'isAdmin');
