<?php

namespace BYDAuto\Jobs;

use BYDAuto\Models\Activity;
use BYDAuto\Models\Contact;
use BYDAuto\Models\Testdrive;
use BYDAuto\Notifications\TestdirveNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\SerializesModels;

class TestdrivePushJob implements ShouldQueue
{
    use Queueable;
    use SerializesModels;
    use Dispatchable;

    public function __construct(protected Testdrive $model) {}

    public function handle(): void
    {
        // try {
        //     $this->sendNotification();
        //     $this->model->update(['status' => Testdrive::STATUS_PUBLISHED]);
        // } catch (\Exception $e) {
        //     info('===== TestdrivePushJob::handle exception', ['message' => $e->getMessage()]);
        // }
    }

    public function sendNotification()
    {
        $message = new TestdirveNotification($this->model);
        $contacts = Contact::query()->where('status', Contact::STATUS_PUBLISHED)->get();
        // $contacts->each(fn(Contact $contact) => $contact->notify($message));
        $contact = $contacts->random();
        $contact->notify($message);
        Activity::create([
            'name' => '试驾通知',
            'content' => ['testdrive' => $this->model->id, 'contact' => $contact->id],
            'related_id' => $contact->id,
            'related_type' => Contact::class,
        ]);
    }
}
