<?php

namespace App\Domain\Identity\Services;

use App\Domain\Identity\Models\Branch;
use App\Domain\Identity\Models\BranchSetting;

class BranchSettingsService
{
    /**
     * Get a setting value for a branch. Falls back to academy-level system_settings.
     */
    public function get(int $branchId, string $key, mixed $default = null): mixed
    {
        $setting = BranchSetting::where('branch_id', $branchId)
            ->where('key', $key)
            ->first();

        if ($setting) {
            return $setting->value;
        }

        // Fall back to academy-level setting
        $academy = app('current_academy');
        if ($academy) {
            $systemSetting = \DB::table('system_settings')
                ->where('academy_id', $academy->id)
                ->where('key', $key)
                ->first();

            if ($systemSetting) {
                return $systemSetting->value;
            }
        }

        return $default;
    }

    /**
     * Set a branch-level setting.
     */
    public function set(int $branchId, string $key, mixed $value, string $group = 'general'): void
    {
        $academy = app('current_academy');

        BranchSetting::updateOrCreate(
            ['branch_id' => $branchId, 'key' => $key],
            [
                'academy_id' => $academy?->id,
                'value' => is_array($value) ? json_encode($value) : (string) $value,
                'group' => $group,
            ]
        );
    }

    /**
     * Get all settings for a branch (merged with academy defaults).
     */
    public function allForBranch(int $branchId): array
    {
        $branchSettings = BranchSetting::where('branch_id', $branchId)
            ->pluck('value', 'key')
            ->toArray();

        $academy = app('current_academy');
        $academySettings = [];
        if ($academy) {
            $academySettings = \DB::table('system_settings')
                ->where('academy_id', $academy->id)
                ->pluck('value', 'key')
                ->toArray();
        }

        // Branch overrides academy
        return array_merge($academySettings, $branchSettings);
    }

    /**
     * Get receipt configuration for a branch.
     */
    public function getReceiptConfig(int $branchId): array
    {
        $settings = BranchSetting::where('branch_id', $branchId)
            ->where('group', 'receipt')
            ->pluck('value', 'key')
            ->toArray();

        return array_merge([
            'receipt_header_show_logo' => 'true',
            'receipt_header_show_branch_name' => 'true',
            'receipt_header_show_branch_address' => 'true',
            'receipt_header_show_branch_phone' => 'true',
            'receipt_show_cashier' => 'true',
            'receipt_show_participant' => 'true',
            'receipt_show_tax' => 'true',
            'receipt_show_discount_details' => 'true',
            'receipt_show_payment_method' => 'true',
            'receipt_show_invoice_number' => 'false',
            'receipt_show_qr' => 'false',
            'receipt_footer_text' => 'شكراً لزيارتكم',
            'receipt_return_policy' => 'لا يمكن استرجاع المبلغ بعد 7 أيام',
            'receipt_paper_width' => '80mm',
        ], $settings);
    }
}
