<?php

namespace App\Mail;

use App\Models\Call;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class CallBlockedMail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    public $call;
    public $callable;
    public string $callableType;
    public string $mailLocale;
    public string $callTitle;
    public string $supportEmail;

    public function __construct(Call $call, $callable, string $callableType)
    {
        $this->call         = $call;
        $this->callable     = $callable;
        $this->callableType = $callableType;

        $this->mailLocale   = $callable->lang_preference ?? config('app.fallback_locale', 'en');
        $this->locale($this->mailLocale);
        $this->callTitle    = $call->tag?->name ?? '';
        $this->supportEmail = timebank_config('mail.support.email', '');
    }

    public function build()
    {
        app()->setLocale($this->mailLocale);

        $callTitle = $this->call->tag?->translation?->name ?? $this->call->tag?->name ?? '';

        return $this
            ->from(
                timebank_config('mail.system_admin.email'),
                timebank_config('mail.system_admin.name')
            )
            ->subject(trans('Your call has been blocked', [], $this->mailLocale))
            ->view('emails.calls.blocked')
            ->with([
                'callable'     => $this->callable,
                'call'         => $this->call,
                'callTitle'    => $callTitle,
                'supportEmail' => $this->supportEmail,
            ]);
    }
}
