<?php

namespace App\Services;

use App\Models\AccountType;
use App\Models\ContractType;
use App\Models\Person\AddressType;
use App\Models\Person\PhoneType;
use Illuminate\Support\Facades\Cache;

class ReferenceDataCache
{
    private const TTL = 3600; // 1 hour

    public function getAddressTypes()
    {
        return Cache::remember('reference_data:address_types', self::TTL, fn () => AddressType::all());
    }

    public function getPhoneTypes()
    {
        return Cache::remember('reference_data:phone_types', self::TTL, fn () => PhoneType::all());
    }

    public function getAccountTypes()
    {
        return Cache::remember('reference_data:account_types', self::TTL, fn () => AccountType::all());
    }

    public function getContractTypes()
    {
        return Cache::remember('reference_data:contract_types', self::TTL, fn () => ContractType::whereNull('deleted_at')->get());
    }

    /**
     * Clear all reference data cache.
     */
    public function clearAll(): void
    {
        Cache::forget('reference_data:address_types');
        Cache::forget('reference_data:phone_types');
        Cache::forget('reference_data:account_types');
        Cache::forget('reference_data:contract_types');
    }

    /**
     * Clear specific reference data cache.
     */
    public function clear(string $type): void
    {
        Cache::forget("reference_data:{$type}");
    }

    /**
     * Get all types as an array for convenience.
     */
    public function getAllTypes(): array
    {
        return [
            'address_types' => $this->getAddressTypes(),
            'phone_types' => $this->getPhoneTypes(),
            'account_types' => $this->getAccountTypes(),
            'contract_types' => $this->getContractTypes(),
        ];
    }
}


