<?php

namespace App;

class ApplicationCategory
{
    // Solar Category ID Constants for Easy Debugging & Reference
    const SOLAR_ROOFTOP_SOLUTIONS = 6;
    const SOLAR_ROOFTOP_PANEL     = 24;
    const SOLAR_CHANNEL_FINANCING  = 52;

    /**
     * Map of Solar Category IDs to Descriptive Names
     *
     * @var array
     */
    public static $solarCategoryNames = [
        self::SOLAR_ROOFTOP_SOLUTIONS => 'Solar and Rooftop Solutions',
        self::SOLAR_ROOFTOP_PANEL     => 'Solar and Rooftop Panel',
        self::SOLAR_CHANNEL_FINANCING  => 'Solar Channel Financing',
    ];

    /**
     * List of all Solar Category IDs
     *
     * @var array
     */
    public static $solarCategories = [
        self::SOLAR_ROOFTOP_SOLUTIONS,
        self::SOLAR_ROOFTOP_PANEL,
        self::SOLAR_CHANNEL_FINANCING,
    ];

    /**
     * Get all active Solar Category IDs (hybrid: checks .env overrides/additions, falls back to constants)
     *
     * @return array
     */
    public static function getSolarCategories()
    {
        $categories = static::$solarCategories;

        // 1. Check for comma-separated env list (e.g. SOLAR_CATEGORY_IDS="6,24,52")
        $envList = function_exists('_env') ? _env('SOLAR_CATEGORY_IDS') : env('SOLAR_CATEGORY_IDS');
        if (!empty($envList)) {
            $ids = array_filter(array_map('trim', explode(',', $envList)));
            if (!empty($ids)) {
                $categories = array_map('intval', $ids);
            }
        }

        // 2. Fallback check for legacy single SOLAR_CATEGORY_ID env if present
        $legacySingle = function_exists('_env') ? _env('SOLAR_CATEGORY_ID') : env('SOLAR_CATEGORY_ID');
        if (!empty($legacySingle) && !in_array((int)$legacySingle, $categories, true)) {
            $categories[] = (int)$legacySingle;
        }

        return array_values(array_unique($categories));
    }

    /**
     * Check if given category ID belongs to Solar
     *
     * @param int|string|null $categoryId
     * @return bool
     */
    public static function isSolarCategory($categoryId)
    {
        if (empty($categoryId)) {
            return false;
        }
        return in_array((int)$categoryId, static::getSolarCategories(), true);
    }

    /**
     * Check if given category ID belongs to Non-Solar
     *
     * @param int|string|null $categoryId
     * @return bool
     */
    public static function isNonSolarCategory($categoryId)
    {
        return !static::isSolarCategory($categoryId);
    }

    /**
     * Get descriptive Solar Category Name if known
     *
     * @param int|string|null $categoryId
     * @return string
     */
    public static function getSolarCategoryName($categoryId)
    {
        $id = (int)$categoryId;
        return isset(static::$solarCategoryNames[$id]) 
            ? static::$solarCategoryNames[$id] 
            : (static::isSolarCategory($id) ? 'Solar Category' : 'Non-Solar / Other Category');
    }
}
