<?php

namespace Fotogenic_AI;

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

final class Messages {
    private const MAP = [
        'missing_auth_session' => [
            'code' => 'fotogenic_ai_missing_auth_session',
            'message' => 'Login to Fotogenic to continue.',
        ],
        'auth_session_expired' => [
            'code' => 'fotogenic_ai_auth_session_expired',
            'message' => 'Your Fotogenic session expired. Please log in again.',
        ],
        'auth_refresh_failed' => [
            'code' => 'fotogenic_ai_auth_refresh_failed',
            'message' => 'Could not refresh your Fotogenic session. Please try again.',
        ],
        'missing_api_key' => [
            'code' => 'fotogenic_ai_missing_auth_session',
            'message' => 'Login to Fotogenic to continue.',
        ],
        'invalid_endpoint' => [
            'code' => 'fotogenic_ai_invalid_endpoint',
            'message' => 'Invalid API endpoint.',
        ],
        'missing_path_param' => [
            'code' => 'fotogenic_ai_missing_path_param',
            'message' => 'Missing required path parameters.',
        ],
        'request_failed' => [
            'code' => 'fotogenic_ai_request_failed',
            'message' => 'Request failed. Please try again.',
        ],
        'invalid_response' => [
            'code' => 'fotogenic_ai_invalid_response',
            'message' => 'Invalid API response.',
        ],
        'invalid_request' => [
            'code' => 'fotogenic_ai_invalid_request',
            'message' => 'Missing required data.',
        ],
        'invalid_nonce' => [
            'code' => 'fotogenic_ai_invalid_nonce',
            'message' => 'Invalid nonce.',
        ],
        'missing_product' => [
            'code' => 'fotogenic_ai_missing_product',
            'message' => 'Product is required.',
        ],
        'forbidden_product' => [
            'code' => 'fotogenic_ai_forbidden',
            'message' => 'You are not allowed to generate for this product.',
        ],
        'missing_job' => [
            'code' => 'fotogenic_ai_missing_job',
            'message' => 'Failed to start generation.',
        ],
        'insufficient_credits' => [
            'code' => 'fotogenic_ai_insufficient_credits',
            'message' => 'Insufficient credits to start generation.',
        ],
        'forbidden_image' => [
            'code' => 'fotogenic_ai_forbidden',
            'message' => 'You are not allowed to modify this image.',
        ],
        'forbidden_remove' => [
            'code' => 'fotogenic_ai_forbidden',
            'message' => 'You are not allowed to remove this image.',
        ],
        'missing_attachment' => [
            'code' => 'fotogenic_ai_invalid_request',
            'message' => 'Missing attachment.',
        ],
        'missing_log' => [
            'code' => 'fotogenic_ai_log_missing',
            'message' => 'Log file not found.',
        ],
        'missing_run' => [
            'code' => 'fotogenic_ai_missing_run',
            'message' => 'Missing run ID.',
        ],
        'woocommerce_not_active' => [
            'code' => 'woocommerce_not_active',
            'message' => 'WooCommerce is not active. Product generation requires WooCommerce.',
        ],
        'bad_request' => [
            'code' => 'fotogenic_ai_http_error',
            'message' => 'Bad request.',
        ],
        'unauthorized' => [
            'code' => 'fotogenic_ai_http_error',
            'message' => 'Unauthorized.',
        ],
        'payment_required' => [
            'code' => 'fotogenic_ai_http_error',
            'message' => 'Payment required.',
        ],
        'forbidden' => [
            'code' => 'fotogenic_ai_http_error',
            'message' => 'Forbidden.',
        ],
        'unprocessable' => [
            'code' => 'fotogenic_ai_http_error',
            'message' => 'Unprocessable request.',
        ],
        'server_error' => [
            'code' => 'fotogenic_ai_http_error',
            'message' => 'Server error.',
        ],
        'service_unavailable' => [
            'code' => 'fotogenic_ai_http_error',
            'message' => 'Service unavailable.',
        ],
        'unexpected_api_error' => [
            'code' => 'fotogenic_ai_http_error',
            'message' => 'Unexpected API error.',
        ],
    ];

    public static function t( string $key, array $args = [], array $opts = [] ): string {
        $entry = self::MAP[ $key ] ?? null;
        $message = is_array( $entry ) ? ( $entry['message'] ?? $key ) : $key;
        $translations = [
            'missing_auth_session'  => __( 'Login to Fotogenic to continue.', 'fotogenic-ai' ),
            'auth_session_expired'  => __( 'Your Fotogenic session expired. Please log in again.', 'fotogenic-ai' ),
            'auth_refresh_failed'   => __( 'Could not refresh your Fotogenic session. Please try again.', 'fotogenic-ai' ),
            'missing_api_key'       => __( 'Login to Fotogenic to continue.', 'fotogenic-ai' ),
            'invalid_endpoint'      => __( 'Invalid API endpoint.', 'fotogenic-ai' ),
            'missing_path_param'    => __( 'Missing required path parameters.', 'fotogenic-ai' ),
            'request_failed'        => __( 'Request failed. Please try again.', 'fotogenic-ai' ),
            'invalid_response'      => __( 'Invalid API response.', 'fotogenic-ai' ),
            'invalid_request'       => __( 'Missing required data.', 'fotogenic-ai' ),
            'invalid_nonce'         => __( 'Invalid nonce.', 'fotogenic-ai' ),
            'missing_product'       => __( 'Product is required.', 'fotogenic-ai' ),
            'forbidden_product'     => __( 'You are not allowed to generate for this product.', 'fotogenic-ai' ),
            'missing_job'           => __( 'Failed to start generation.', 'fotogenic-ai' ),
            'insufficient_credits'  => __( 'Insufficient credits to start generation.', 'fotogenic-ai' ),
            'forbidden_image'       => __( 'You are not allowed to modify this image.', 'fotogenic-ai' ),
            'forbidden_remove'      => __( 'You are not allowed to remove this image.', 'fotogenic-ai' ),
            'missing_attachment'    => __( 'Missing attachment.', 'fotogenic-ai' ),
            'missing_log'           => __( 'Log file not found.', 'fotogenic-ai' ),
            'missing_run'           => __( 'Missing run ID.', 'fotogenic-ai' ),
            'woocommerce_not_active'=> __( 'WooCommerce is not active. Product generation requires WooCommerce.', 'fotogenic-ai' ),
            'bad_request'           => __( 'Bad request.', 'fotogenic-ai' ),
            'unauthorized'          => __( 'Unauthorized.', 'fotogenic-ai' ),
            'payment_required'      => __( 'Payment required.', 'fotogenic-ai' ),
            'forbidden'             => __( 'Forbidden.', 'fotogenic-ai' ),
            'unprocessable'         => __( 'Unprocessable request.', 'fotogenic-ai' ),
            'server_error'          => __( 'Server error.', 'fotogenic-ai' ),
            'service_unavailable'   => __( 'Service unavailable.', 'fotogenic-ai' ),
            'unexpected_api_error'  => __( 'Unexpected API error.', 'fotogenic-ai' ),
        ];
        $translated = $translations[ $key ] ?? $message;
        if ( empty( $args ) ) {
            return $translated;
        }
        $safe_args = array_map( static fn( $value ) => (string) $value, $args );
        return vsprintf( $translated, $safe_args );
    }

    public static function error( string $key, int $status = 400, array $args = [], array $data = [] ): \WP_Error {
        $entry = self::MAP[ $key ] ?? null;
        $code = is_array( $entry ) ? ( $entry['code'] ?? $key ) : $key;
        $message = self::t( $key, $args );
        $payload = array_merge( [ 'status' => $status ], $data );
        return new \WP_Error( $code, $message, $payload );
    }
}
