<?php

namespace App\Services;

use App\Models\Business;
use App\Models\Setting;

class ModuleManager
{
    public const MODULES = [
        'fixed_assets' => [
            'label' => 'Fixed Assets',
            'description' => 'Asset register, depreciation, maintenance and fixed-asset reports.',
            'default' => true,
        ],
        'financial_reports' => [
            'label' => 'Financial Reports',
            'description' => 'Trial balance, financial statements, cash flow and portfolio reports.',
            'default' => true,
        ],
        'tasks' => [
            'label' => 'Tasks',
            'description' => 'Staff task assignment, collection follow-up tasks and task workflow management.',
            'default' => true,
        ],
        'calendar' => [
            'label' => 'Calendar',
            'description' => 'Management calendar, appointments and operational schedule visibility.',
            'default' => true,
        ],
        'loan_writeoffs' => [
            'label' => 'Loan Write-off',
            'description' => 'Loan write-off requests, approvals, evidence, accounting and recoveries.',
            'default' => true,
        ],
        'payroll' => [
            'label' => 'Payroll',
            'description' => 'Employee compensation, payroll periods, approvals, payments and payslips.',
            'default' => true,
        ],
        'remote_payments' => [
            'label' => 'Remote Payments',
            'description' => 'Remote payment submission, proof review, verification, approval and rejection workflow.',
            'default' => true,
        ],
        'sms_messaging' => [
            'label' => 'SMS Messaging',
            'description' => 'SMS repayment reminders and system SMS delivery through the configured SMS provider.',
            'default' => true,
        ],
        'whatsapp_messaging' => [
            'label' => 'WhatsApp Messaging',
            'description' => 'WhatsApp repayment reminders and system WhatsApp delivery through the configured provider.',
            'default' => true,
        ],
    ];

    public function enabled(string $module, ?int $businessId = null): bool
    {
        if (! isset(self::MODULES[$module])) {
            return false;
        }

        $businessId ??= (int) (Business::query()->value('id') ?: 1);
        $value = Setting::query()
            ->where('business_id', $businessId)
            ->where('group', 'modules')
            ->where('key', $module)
            ->value('value');

        if ($value === null) {
            return (bool) self::MODULES[$module]['default'];
        }

        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
    }

    public function all(?int $businessId = null): array
    {
        return collect(self::MODULES)->map(function (array $definition, string $key) use ($businessId): array {
            return $definition + ['key' => $key, 'enabled' => $this->enabled($key, $businessId)];
        })->all();
    }
}
