<?php
/**
 * Admin Settings – Client Feedback Fields Configuration
 * Allows admin to map Asana custom fields for client feedback (rating, reply, feedback text)
 */

if (!defined('ABSPATH')) exit;
if (!current_user_can('manage_options')) wp_die('Unauthorized');

$feedback_fields = afb_get_feedback_field_gids();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['afb_save_feedback_settings'])) {
    check_admin_referer('afb_save_feedback_settings');

    $rating_gid    = preg_replace('/\D+/', '', (string)($_POST['afb_feedback_rating_gid'] ?? ''));
    $reply_gid     = preg_replace('/\D+/', '', (string)($_POST['afb_feedback_reply_gid'] ?? ''));
    $feedback_gid  = preg_replace('/\D+/', '', (string)($_POST['afb_feedback_feedback_gid'] ?? ''));
    $status_gid    = preg_replace('/\D+/', '', (string)($_POST['afb_feedback_status_gid'] ?? ''));

    // Rating enum option GIDs (one per star value, 1–5)
    $rating_enum_gids = [];
    for ($i = 1; $i <= 5; $i++) {
        $gid = preg_replace('/\D+/', '', (string)($_POST["afb_rating_enum_gid_{$i}"] ?? ''));
        $rating_enum_gids[] = $gid; // preserve index even if blank — order matters
    }

    // Status field's "Submitted" enum option GID
    $status_submitted_enum_gid = preg_replace('/\D+/', '', (string)($_POST['afb_status_submitted_enum_gid'] ?? ''));

    update_option('afb_feedback_rating_field_gid',    $rating_gid);
    update_option('afb_feedback_reply_field_gid',      $reply_gid);
    update_option('afb_feedback_feedback_field_gid',   $feedback_gid);
    update_option('afb_feedback_status_field_gid',     $status_gid);
    update_option('afb_feedback_rating_enum_gids',     json_encode($rating_enum_gids));
    update_option('afb_status_submitted_enum_gid',     $status_submitted_enum_gid);

    echo '<div class="notice notice-success is-dismissible"><p>Feedback field settings saved.</p></div>';
    $feedback_fields = afb_get_feedback_field_gids();
}

?>

<div class="wrap">
    <h1>Client Feedback Settings</h1>
    <p class="description" style="max-width:900px; margin-top: 6px;">
        Configure which Asana custom fields will store client feedback (rating, reply, feedback text, and submission status).
        These fields will be populated when clients submit feedback after task completion.
    </p>

    <form method="post">
        <?php wp_nonce_field('afb_save_feedback_settings'); ?>
        
        <table class="form-table" style="max-width:900px; margin-top: 12px;">
            <tr>
                <th scope="row">
                    <label for="afb_feedback_rating_gid">Rating Field GID</label>
                </th>
                <td>
                    <input 
                        type="text"
                        id="afb_feedback_rating_gid"
                        name="afb_feedback_rating_gid"
                        value="<?= esc_attr($feedback_fields['rating_gid'] ?? '') ?>"
                        placeholder="e.g., 1234567890123456"
                        style="width:300px;font-family:monospace"
                    />
                    <p class="description">
                        Asana custom field GID for client rating. Should be a single-select enum field with options 1–5.
                    </p>

                    <?php
                    $saved_enum_gids = json_decode(get_option('afb_feedback_rating_enum_gids', '[]'), true) ?: [];
                    $star_labels = ['1 Star', '2 Stars', '3 Stars', '4 Stars', '5 Stars'];
                    ?>
                    <table style="margin-top:10px;border-collapse:collapse">
                        <thead>
                            <tr>
                                <th style="text-align:left;font-weight:600;font-size:.8rem;color:#6b7280;padding:0 12px 4px 0">Star value</th>
                                <th style="text-align:left;font-weight:600;font-size:.8rem;color:#6b7280;padding:0 0 4px 0">Enum option GID</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php for ($i = 1; $i <= 5; $i++) : ?>
                            <tr>
                                <td style="padding:3px 12px 3px 0;font-size:.85rem;color:#374151"><?= esc_html($star_labels[$i - 1]) ?></td>
                                <td style="padding:3px 0">
                                    <input
                                        type="text"
                                        name="afb_rating_enum_gid_<?= $i ?>"
                                        value="<?= esc_attr($saved_enum_gids[$i - 1] ?? '') ?>"
                                        placeholder="e.g., 1234567890123456"
                                        style="width:240px;font-family:monospace;font-size:.85rem"
                                    />
                                </td>
                            </tr>
                        <?php endfor; ?>
                        </tbody>
                    </table>
                    <p class="description" style="margin-top:6px">
                        Enter the GID for each star's enum option (in order). Leave blank to disable rating storage.
                    </p>
                </td>
            </tr>

            <tr>
                <th scope="row">
                    <label for="afb_feedback_reply_gid">Reply Field GID</label>
                </th>
                <td>
                    <input 
                        type="text"
                        id="afb_feedback_reply_gid"
                        name="afb_feedback_reply_gid"
                        value="<?= esc_attr($feedback_fields['reply_gid'] ?? '') ?>"
                        placeholder="e.g., 1234567890123456"
                        style="width:300px;font-family:monospace"
                    />
                    <p class="description">
                        Asana custom field GID for client's quick reply. Should be a text field.
                    </p>
                </td>
            </tr>

            <tr>
                <th scope="row">
                    <label for="afb_feedback_feedback_gid">Feedback Text Field GID</label>
                </th>
                <td>
                    <input 
                        type="text"
                        id="afb_feedback_feedback_gid"
                        name="afb_feedback_feedback_gid"
                        value="<?= esc_attr($feedback_fields['feedback_gid'] ?? '') ?>"
                        placeholder="e.g., 1234567890123456"
                        style="width:300px;font-family:monospace"
                    />
                    <p class="description">
                        Asana custom field GID for longer feedback text. Should be a text field.
                    </p>
                </td>
            </tr>

            <tr>
                <th scope="row">
                    <label for="afb_feedback_status_gid">Feedback Status Field GID</label>
                </th>
                <td>
                    <input 
                        type="text"
                        id="afb_feedback_status_gid"
                        name="afb_feedback_status_gid"
                        value="<?= esc_attr($feedback_fields['status_gid'] ?? '') ?>"
                        placeholder="e.g., 1234567890123456"
                        style="width:300px;font-family:monospace"
                    />
                    <p class="description">
                        Asana custom field GID for feedback status. Should be a single-select enum field.
                    </p>

                    <label for="afb_status_submitted_enum_gid" style="display:block;margin-top:10px;font-weight:600;font-size:.9rem">
                        "Submitted" Enum Option GID
                    </label>
                    <input 
                        type="text"
                        id="afb_status_submitted_enum_gid"
                        name="afb_status_submitted_enum_gid"
                        value="<?= esc_attr(get_option('afb_status_submitted_enum_gid', '')) ?>"
                        placeholder="e.g., 1234567890123456"
                        style="width:300px;font-family:monospace;margin-top:4px"
                    />
                    <p class="description" style="margin-top:4px">
                        The GID of the "Submitted" enum option within the feedback status field. Will be set when client submits feedback.
                    </p>
                </td>
            </tr>
        </table>

        <div style="background:#f3f4f6;border-left:4px solid #3b82f6;padding:1rem;margin:1.5rem 0;max-width:900px">
            <p style="margin:0;font-size:.875rem;color:#1f2937">
                <strong>How to find field GIDs:</strong><br>
                Open the Asana project settings → Custom fields. Click on a field to see its GID in the URL or details panel.
                Example GID format: <code>1234567890123456</code> (16 digits, all numbers)
            </p>
        </div>

        <p style="margin-top: 14px; max-width: 900px;" class="description">
            Leave any field empty to disable it. At least one field should be configured for feedback collection.
        </p>

        <p style="margin-top: 12px;">
            <button type="submit" name="afb_save_feedback_settings" class="button button-primary">Save Settings</button>
        </p>
    </form>
</div>
