<?php

namespace App\Jobs;

use App\Mail\CharacteristicApprovalMail;
use App\Models\CharacteristicApproverMaster;
use App\Models\RequestCharacteristic;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Mail;

class SendApprover3MailJob implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new job instance.
     */
    // public function __construct()
    // {
    //     //
    // }

    /**
     * Execute the job.
     */
    public function __construct(public RequestCharacteristic $request) {}

    public function handle()
    {
        if (
            !is_null($this->request->approver_status1) ||
            !is_null($this->request->approver_status2) ||
            !is_null($this->request->approver_status3)
        ) {
            return;
        }

        if ($this->request->approver3_mail_sent) return;

        $approver = CharacteristicApproverMaster::where('plant_id', $this->request->plant_id)
            ->where('machine_id', $this->request->machine_id)
            ->first();

        if (! $approver || ! $approver->mail3) return;

        Mail::to($approver->mail3)
            ->queue(new CharacteristicApprovalMail(
                $this->request,
                $approver->name3,
                3
            ));

        $this->request->update(['approver3_mail_sent' => 1]);
    }
}
