<?php

namespace App\Support;

use App\Models\SystemSetting;
use App\Http\Controllers\FilesMainController;
use App\Support\AccountingTheme;

class SystemSettingsHelper
{
    /**
     * Get application name.
     */
    public static function appName(): string
    {
        return SystemSetting::getValue('app_name', 'BukidBounty');
    }

    /**
     * Get application description.
     */
    public static function appDescription(): string
    {
        return SystemSetting::getValue('app_description', 'Agricultural Management Platform & Marketplace');
    }

    /**
     * Get application tagline.
     */
    public static function appTagline(): string
    {
        return SystemSetting::getValue('app_tagline', 'Bounty of the Fields at Your Fingertips');
    }

    /**
     * Get logo URL.
     */
    public static function logoUrl(): string
    {
        $hashkey = SystemSetting::getValue('app_logo');
        if ($hashkey) {
            return FilesMainController::generateURLforFileListHash($hashkey);
        }
        return cdn_asset('vendor/assets/icons/192x192.png');
    }

    /**
     * Get primary theme color.
     */
    public static function primaryColor(): string
    {
        return SystemSetting::getValue('primary_color', '#0d6efd');
    }

    /**
     * Get footer text.
     */
    public static function footerText(): string
    {
        return SystemSetting::getValue('footer_text', '&copy; 2026 BukidBounty Ecosystem. All rights reserved.');
    }

    /**
     * Check if maintenance mode is active.
     */
    public static function isMaintenanceMode(): bool
    {
        return SystemSetting::getValue('maintenance_mode', false);
    }

    /**
     * Get the application mode (corporate, cooperative, ngo, others).
     */
    public static function appMode(): string
    {
        return SystemSetting::getValue('app_mode', 'corporate');
    }

    /**
     * Get the hashkey of the main cooperative/organization (if any).
     */
    public static function mainOrganizationHashkey(): ?string
    {
        $value = SystemSetting::getValue('main_organization');
        return $value ?: null;
    }

    /**
     * Get the main cooperative/organization model (if any).
     */
    public static function mainOrganization(): ?\App\Models\Market\Organization
    {
        $hashkey = static::mainOrganizationHashkey();
        if (!$hashkey) {
            return null;
        }
        return \App\Models\Market\Organization::where('hashkey', $hashkey)->first();
    }

    /**
     * Get the active accounting theme key.
     */
    public static function accountingTheme(): string
    {
        return SystemSetting::getValue('accounting_theme', AccountingTheme::DEFAULT_KEY);
    }

    /**
     * Get available chapter/org position titles.
     */
    public static function chapterPositions(): array
    {
        $value = SystemSetting::getValue('chapter_positions');
        if (!$value) {
            return ['National Director', 'Regional Director', 'Provincial Coordinator', 'City/Municipal Officer', 'Barangay Captain', 'Secretary', 'Treasurer', 'Auditor', 'Member'];
        }
        return is_string($value) ? (json_decode($value, true) ?? []) : (array) $value;
    }
}
