<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

/**
 * Environment URLs — switch deployment.target in .env:
 *   dev  = data.busvalley.net (test)
 *   live = data.busvalley.com (production)
 */
class Deployment extends BaseConfig
{
    /** @var string */
    public $target = 'dev';

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

        $target = strtolower(trim((string) env('deployment.target', 'dev')));
        $this->target = in_array($target, ['dev', 'live'], true) ? $target : 'dev';
    }

    public function isDev(): bool
    {
        return $this->target === 'dev';
    }

    public function isLive(): bool
    {
        return $this->target === 'live';
    }

    /**
     * Admin + API base URL (CodeIgniter app.baseURL).
     * Default: data.busvalley.net (override via deployment.apiBaseUrl.{target} in .env).
     */
    public function apiBaseUrl(): string
    {
        $override = trim((string) env('deployment.apiBaseUrl.' . $this->target, ''));
        if ($override !== '') {
            return $this->normalizeBaseUrl($override);
        }

        // Prefer explicit env override for either target; otherwise always .net
        $forced = trim((string) env('app.baseURL', ''));
        if ($forced !== '') {
            return $this->normalizeBaseUrl($forced);
        }

        return $this->normalizeBaseUrl('https://data.busvalley.net/busvalley_new');
    }

    /**
     * Customer website (good.busvalley.*).
     */
    public function websiteUrl(): string
    {
        $override = trim((string) env('deployment.websiteUrl.' . $this->target, ''));
        if ($override !== '') {
            return rtrim($override, '/');
        }

        return $this->target === 'live'
            ? 'https://good.busvalley.com'
            : 'https://good.busvalley.net';
    }

    /**
     * Public marketing site (busvalley.*).
     */
    public function publicSiteUrl(): string
    {
        $override = trim((string) env('deployment.publicUrl.' . $this->target, ''));
        if ($override !== '') {
            return rtrim($override, '/');
        }

        return $this->target === 'live'
            ? 'https://busvalley.com'
            : 'https://busvalley.net';
    }

    /**
     * CORS allowed origins for the API filter.
     *
     * Includes both .net (test) and .com (live) frontends so the website can call
     * the dev API during cross-environment testing (e.g. good.busvalley.com → data.busvalley.net).
     *
     * @return list<string>
     */
    public function allowedCorsOrigins(): array
    {
        $local = [
            'http://localhost:3000',
            'http://localhost:3002',
        ];

        $busvalley = [
            'https://busvalley.com',
            'https://www.busvalley.com',
            'https://busvalley.net',
            'https://www.busvalley.net',
            'https://data.busvalley.net',
            'https://www.data.busvalley.net',
            'https://good.busvalley.net',
            'https://www.good.busvalley.net',
            'https://data.busvalley.com',
            'https://www.data.busvalley.com',
            'https://good.busvalley.com',
            'https://www.good.busvalley.com',
        ];

        return array_values(array_unique(array_merge($local, $busvalley)));
    }

    private function normalizeBaseUrl(string $url): string
    {
        return rtrim($url, '/') . '/';
    }
}
