<?php

namespace App\Mailables;

use App\Models\SubscriptionPlanOption;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Lang;

class SubscriptionContactEmail extends Mailable
{
    use Queueable, SerializesModels;

    public User $user;
    public string $requestType;
    public ?int $targetPlanOptionId;

    /**
     * Create a new message instance.
     */
    public function __construct(User $user, string $requestType, int $targetPlanOptionId = null)
    {
        $this->user = $user;
        $this->requestType = $requestType;
        $this->targetPlanOptionId = $targetPlanOptionId;
    }

    /**
     * Build the message.
     */
    public function build()
    {
        $account = $this->user->ownAccount;
        $subscription = $account->activeSubscriptions()->first();
        $targetPlanOption = $this->targetPlanOptionId ? SubscriptionPlanOption::find($this->targetPlanOptionId) : null;
        $subscriptionPlan = $targetPlanOption ? $targetPlanOption->subscriptionPlan : null;

        return $this->view('emails.subscription-contact')
            ->subject(Lang::get('email.subject_subscription_contact'))
            ->with([
                'requestType' => $this->requestType,
                'userName' => $this->user->first_name . ' ' . $this->user->last_name,
                'userEmail' => $this->user->email,
                'accountCompany' => $account->company,
                'subscriptionPlan' => $subscription->subscriptionPlan->name,
                'targetSubscriptionPlan' => $subscriptionPlan ? $subscriptionPlan->name : null,
                'targetPlanOption' => $targetPlanOption ? $targetPlanOption->name : null,
            ]);
    }
}
