<?php

namespace App\Mail;

use App\Models\Transaction;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class TransferReceived extends Mailable implements ShouldQueue  // ShouldQueue here creates the class as a background job
{
    use Queueable;
    use SerializesModels;

    public $transaction;
    public $locale;


    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(Transaction $transaction, $messageLocale, )     // Binds the Transaction model to $transaction
    {
        $this->transaction = $transaction;      
        $this->locale = $messageLocale;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        // Set application locale for this email
        app()->setLocale($this->locale);

        $recipient = $this->transaction->accountTo->accountable;

        // Generate the transaction history URL
        $transactionsRoute = LaravelLocalization::getURLFromRouteNameTranslated($this->locale, 'routes.transactions');

        // Generate appropriate URL based on recipient type
        // For organizations, banks, and admins, use direct login URL
        if ($recipient instanceof \App\Models\Organization) {
            $baseUrl = route('organization.direct-login', [
                'organizationId' => $recipient->id,
                'intended' => $transactionsRoute
            ]);
            $transactionHistoryUrl = LaravelLocalization::localizeURL($baseUrl, $this->locale);
        } elseif ($recipient instanceof \App\Models\Bank) {
            $baseUrl = route('bank.direct-login', [
                'bankId' => $recipient->id,
                'intended' => $transactionsRoute
            ]);
            $transactionHistoryUrl = LaravelLocalization::localizeURL($baseUrl, $this->locale);
        } elseif ($recipient instanceof \App\Models\Admin) {
            $baseUrl = route('admin.direct-login', [
                'adminId' => $recipient->id,
                'intended' => $transactionsRoute
            ]);
            $transactionHistoryUrl = LaravelLocalization::localizeURL($baseUrl, $this->locale);
        } else {
            // For regular users, just use the transactions URL
            $transactionHistoryUrl = $transactionsRoute;
        }

        // Generate the transaction statement URL
        $statementRoute = LaravelLocalization::getURLFromRouteNameTranslated($this->locale, 'routes.statement', array('transactionId' => $this->transaction->id));

        // Generate appropriate URL for statement based on recipient type
        if ($recipient instanceof \App\Models\Organization) {
            $baseUrl = route('organization.direct-login', [
                'organizationId' => $recipient->id,
                'intended' => $statementRoute
            ]);
            $transactionStatement = LaravelLocalization::localizeURL($baseUrl, $this->locale);
        } elseif ($recipient instanceof \App\Models\Bank) {
            $baseUrl = route('bank.direct-login', [
                'bankId' => $recipient->id,
                'intended' => $statementRoute
            ]);
            $transactionStatement = LaravelLocalization::localizeURL($baseUrl, $this->locale);
        } elseif ($recipient instanceof \App\Models\Admin) {
            $baseUrl = route('admin.direct-login', [
                'adminId' => $recipient->id,
                'intended' => $statementRoute
            ]);
            $transactionStatement = LaravelLocalization::localizeURL($baseUrl, $this->locale);
        } else {
            // For regular users, just use the statement URL
            $transactionStatement = $statementRoute;
        }

        $senderName = $this->transaction->accountFrom->accountable->name;

        return $this
            ->from(timebank_config('mail.payments.email'), timebank_config('mail.payments.name'))
            ->subject(trans('messages.Payment_received_from', ['name' => $senderName], $this->locale))
            ->view('emails.transfers.payment-received')
            ->with([
                'transaction' => $this->transaction,
                'transactionHistoryUrl' => $transactionHistoryUrl,
                'transactionStatement' => $transactionStatement,
            ]);
    }
}
