<?php

declare(strict_types=1);

namespace Kylin\Agriculture\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Kylin\Agriculture\Models\Sensor;
use Kylin\Agriculture\Models\WarningRule;

class SendWarningNotification implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public int $tries = 5;
    public int $timeout = 120;

    public function __construct(
        public Sensor $sensor,
        public WarningRule $rule,
        public float $value,
    ) {}

    public function handle(): void
    {
        $channels = config('kylin-agriculture.warning_channels', ['database']);

        foreach ($channels as $channel) {
            match ($channel) {
                'database' => $this->sendDatabaseNotification(),
                'wechat' => $this->sendWechatNotification(),
                'sms' => $this->sendSmsNotification(),
                default => null,
            };
        }
    }

    private function sendDatabaseNotification(): void
    {
        Log::info('预警通知（站内消息）', [
            'sensor' => $this->sensor->device_id,
            'rule' => $this->rule->name,
            'value' => $this->value,
        ]);
    }

    private function sendWechatNotification(): void
    {
        Log::info('预警通知（微信推送）', [
            'sensor' => $this->sensor->device_id,
            'rule' => $this->rule->name,
        ]);
    }

    private function sendSmsNotification(): void
    {
        Log::info('预警通知（短信推送）', [
            'sensor' => $this->sensor->device_id,
            'rule' => $this->rule->name,
        ]);
    }
}
