<?php

namespace App\Providers;

use App\Mail\MailketingTransport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Gate::define('admin', function ($user) {
            return $user && method_exists($user, 'isAdmin') ? $user->isAdmin() : false;
        });

        Mail::extend('mailketing', function (array $config) {
            return new MailketingTransport(
                apiToken: (string) ($config['token'] ?? ''),
                endpoint: (string) ($config['endpoint'] ?? 'https://api.mailketing.co.id/api/v1/send'),
                defaultFromName: config('mail.from.name'),
                defaultFromEmail: config('mail.from.address'),
                timeoutSeconds: (int) ($config['timeout'] ?? 10),
            );
        });
    }
}
