<?php

return [
    'paths' => ['api/*', 'public/api/*', 'sanctum/csrf-cookie'],
    'allowed_methods' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
    'allowed_origins' => (function () {
        $normalize = function ($origin) {
            $origin = trim((string) $origin);
            if ($origin === '') {
                return '';
            }

            return rtrim($origin, '/');
        };

        $raw = env('ASUP_CORS_ALLOWED_ORIGINS', '');
        if (is_string($raw)) {
            $items = array_values(array_filter(array_map(fn ($x) => $normalize($x), explode(',', $raw)), fn ($x) => $x !== '' && $x !== '*'));
            if (count($items) > 0) {
                return $items;
            }
        }

        $frontend = $normalize(env('ASUP_FRONTEND_URL', ''));
        if ($frontend !== '') {
            return [$frontend];
        }

        $env = (string) env('APP_ENV', 'production');
        $debug = (bool) env('APP_DEBUG', false);
        if ($env === 'local' || $debug) {
            return ['http://localhost:5173', 'http://127.0.0.1:5173', 'http://localhost:3000', 'http://127.0.0.1:3000'];
        }

        return [];
    })(),
    'allowed_origins_patterns' => (function () {
        $raw = env('ASUP_CORS_ALLOWED_ORIGINS_PATTERNS', '');
        if (is_string($raw)) {
            $items = array_values(array_filter(array_map(fn ($x) => trim((string) $x), explode(',', $raw)), fn ($x) => $x !== ''));
            if (count($items) > 0) {
                return $items;
            }
        }

        $env = (string) env('APP_ENV', 'production');
        $debug = (bool) env('APP_DEBUG', false);
        if ($env === 'local' || $debug) {
            return [];
        }

        return [
            '#^https://([a-z0-9-]+\.)?asup\.edu\.pe$#i',
            '#^https://([a-z0-9-]+\.)?nextboostperu\.com$#i',
        ];
    })(),
    'allowed_headers' => ['Content-Type', 'Authorization', 'Accept', 'X-Requested-With'],
    'exposed_headers' => [],
    'max_age' => 0,
    'supports_credentials' => true,
];
