<?php

declare(strict_types=1);

namespace App\Modules\Interaction;

/**
 * InteractionValidator — Validator for guest RSVPs and wishes.
 *
 * Scoped only to raw input validation against guest pax and content thresholds.
 *
 * @package Inveetaire
 * @ref     PILOT-005
 */
class InteractionValidator
{
    /**
     * Validate RSVP input parameters.
     *
     * @param array      $data             Input array containing 'response', 'confirmed_pax', and optional 'notes'/'message'.
     * @param int        $invitedPax       Max allowed pax for this guest.
     * @param array      $invitationConfig Config containing deadline and edit permissions.
     * @param array|null $existingRsvp     Existing RSVP record if updating.
     * @return array<string, string> Keyed validation errors; empty if valid.
     */
    public function validateRsvp(array $data, int $invitedPax, array $invitationConfig = [], ?array $existingRsvp = null): array
    {
        $errors = [];

        // 0. Check Deadline Expiration
        $deadline = $invitationConfig['rsvp_closes_at'] ?? ($invitationConfig['rsvp_deadline'] ?? null);
        if (!empty($deadline)) {
            $deadlineTime = strtotime((string) $deadline);
            if ($deadlineTime !== false && time() > $deadlineTime) {
                $errors['deadline'] = 'RSVP submission is closed as the deadline has passed.';
                return $errors;
            }
        }

        // 0b. Check Allow Edit Permission
        if ($existingRsvp !== null) {
            $allowEdit = $invitationConfig['rsvp_allow_edit'] ?? ($invitationConfig['allow_edit'] ?? true);
            if (!$allowEdit) {
                $errors['edit_permission'] = 'Editing your RSVP response is not allowed.';
                return $errors;
            }
        }

        // 1. Response validation
        $response = isset($data['response']) ? trim((string) $data['response']) : '';
        if ($response === '') {
            $errors['response'] = 'Please select your attendance status.';
        } elseif (!in_array($response, ['attending', 'declined'], true)) {
            $errors['response'] = 'Invalid attendance selection.';
        }

        // 2. Pax validation
        if ($response === 'attending') {
            $paxVal = isset($data['confirmed_pax']) ? $data['confirmed_pax'] : '';
            $paxInt = filter_var($paxVal, FILTER_VALIDATE_INT);
            if ($paxInt === false || $paxInt < 1 || $paxInt > $invitedPax) {
                $errors['confirmed_pax'] = "Number of attendees must be between 1 and {$invitedPax}.";
            }
        }

        // 3. Guest Notes validation (max 500 characters)
        $notes = isset($data['notes']) ? trim((string) $data['notes']) : (isset($data['message']) ? trim((string) $data['message']) : '');
        if ($notes !== '' && mb_strlen($notes) > 500) {
            $errors['notes'] = 'Guest notes must not exceed 500 characters.';
        }

        return $errors;
    }

    /**
     * Validate Wish congratulatory message.
     *
     * @param array $data         Input array containing 'message'.
     * @param int   $maxCharLimit Maximum character length allowed.
     * @return array<string, string> Keyed validation errors; empty if valid.
     */
    public function validateWish(array $data, int $maxCharLimit = 1000): array
    {
        $errors = [];

        $message = isset($data['message']) ? trim((string) $data['message']) : '';
        if ($message === '') {
            $errors['message'] = 'Congratulatory message cannot be blank.';
        } elseif (mb_strlen($message) > $maxCharLimit) {
            $errors['message'] = "Congratulatory message must not exceed {$maxCharLimit} characters.";
        }

        return $errors;
    }
}
