<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

class ApiCors extends BaseConfig
{
    /**
     * Allowed frontend origins for API CORS (from Deployment config).
     *
     * @var string[]
     */
    public $allowedOrigins = [];

    /**
     * Allowed CORS methods.
     *
     * @var string[]
     */
    public $allowedMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];

    /**
     * Allowed CORS headers.
     *
     * @var string[]
     */
    public $allowedHeaders = ['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Authorization'];

    /**
     * Exposed CORS headers.
     *
     * @var string[]
     */
    public $exposedHeaders = ['Content-Length', 'Content-Type'];

    /**
     * Allow browser credentials for whitelisted origins.
     *
     * @var bool
     */
    public $supportsCredentials = true;

    /**
     * Preflight cache duration.
     *
     * @var int
     */
    public $maxAge = 600;

    public function __construct()
    {
        parent::__construct();

        $this->allowedOrigins = config(Deployment::class)->allowedCorsOrigins();
    }
}
