<?php

namespace App\Providers;

use App\Models\Admin;
use App\Models\Client;
use App\Models\ClientCustomField;
use App\Models\CommunicationTemplate;
use App\Models\CustomReport;
use App\Models\EmailCampaign;
use App\Models\EmailMarketingList;
use App\Models\EmailTemplate;
use App\Models\Quote;
use App\Models\EscalationRule;
use App\Models\InvoiceBilling;
use App\Models\Log;
use App\Models\Product;
use App\Models\ProductCategory;
use App\Models\ProductConfigOption;
use App\Models\ProductConfigOptionGroup;
use App\Models\ProductGroup;
use App\Models\SmsMessage;
use App\Models\NotificationTemplate;
use App\Models\SmsTemplate;
use App\Models\TaxRule;
use App\Models\TicketStatus;
use App\Models\Tld;
use App\Policies\AdminManagementPolicy;
use App\Policies\ClientCustomFieldPolicy;
use App\Policies\ClientPolicy;
use App\Policies\CommunicationTemplatePolicy;
use App\Policies\CustomReportPolicy;
use App\Policies\EmailCampaignPolicy;
use App\Policies\EmailMarketingListPolicy;
use App\Policies\QuotePolicy;
use App\Policies\EscalationRulePolicy;
use App\Policies\InvoiceBillingPolicy;
use App\Policies\LogPolicy;
use App\Policies\PermissionManagementPolicy;
use App\Policies\ProductCategoryPolicy;
use App\Policies\ProductConfigOptionGroupPolicy;
use App\Policies\ProductConfigOptionPolicy;
use App\Policies\ProductGroupPolicy;
use App\Policies\ProductPolicy;
use App\Policies\RoleManagementPolicy;
use App\Policies\SmsMessagePolicy;
use App\Policies\TaxPolicy;
use App\Policies\TicketStatusPolicy;
use App\Policies\TldPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The model to policy mappings for the application.
     *
     * @var array<class-string, class-string>
     */
    protected $policies = [
        Client::class => ClientPolicy::class,
        ClientCustomField::class => ClientCustomFieldPolicy::class,
        ProductGroup::class => ProductGroupPolicy::class,
        Product::class => ProductPolicy::class,
        ProductCategory::class => ProductCategoryPolicy::class,
        ProductConfigOption::class => ProductConfigOptionPolicy::class,
        ProductConfigOptionGroup::class => ProductConfigOptionGroupPolicy::class,
        ProductGroup::class => ProductGroupPolicy::class,
        Tld::class => TldPolicy::class,
        EscalationRule::class => EscalationRulePolicy::class,
        TicketStatus::class => TicketStatusPolicy::class,
        Admin::class => AdminManagementPolicy::class,
        // PRC-619 — birleşik şablon policy'si; alt sınıf instance'ları da aynı policy'ye düşer
        CommunicationTemplate::class => CommunicationTemplatePolicy::class,
        EmailTemplate::class => CommunicationTemplatePolicy::class,
        SmsTemplate::class => CommunicationTemplatePolicy::class,
        NotificationTemplate::class => CommunicationTemplatePolicy::class,
        SmsMessage::class => SmsMessagePolicy::class,
        TaxRule::class => TaxPolicy::class,
        \Spatie\Permission\Models\Role::class => RoleManagementPolicy::class,
        \Spatie\Permission\Models\Permission::class => PermissionManagementPolicy::class,
        Log::class => LogPolicy::class,
        InvoiceBilling::class => InvoiceBillingPolicy::class,
        EmailMarketingList::class => EmailMarketingListPolicy::class,
        EmailCampaign::class => EmailCampaignPolicy::class,
        Quote::class => QuotePolicy::class,
        CustomReport::class => CustomReportPolicy::class,
    ];

    /**
     * Register any authentication / authorization services.
     */
    public function boot(): void
    {
        // PRC-575 — Stale session (auth.session / AuthenticateSession) logout'u
        // varsayılan olarak boş 401 döndürüyordu (kendi redirectTo'su null).
        // Login URL çözümünü Authenticate middleware ile paylaştır → 401 yerine
        // login'e 302 redirect (intended URL stash'lenir, login sonrası geri döner).
        \Illuminate\Session\Middleware\AuthenticateSession::redirectUsing(
            fn (\Illuminate\Http\Request $request) => \App\Http\Middleware\Authenticate::resolveLoginRedirect($request)
        );

        // Admin guard için Gate tanımlamaları
        Gate::define('view-client', function ($admin, $client) {
            return $admin->can('view', $client);
        });

        Gate::define('edit-client', function ($admin, $client) {
            return $admin->can('update', $client);
        });

        Gate::define('delete-client', function ($admin, $client) {
            return $admin->can('delete', $client);
        });
    }
}
