<?php

namespace BYDAuto\Commands;

use App\Mail\HelloMail;
use BYDAuto\Models\Activity;
use BYDAuto\Models\Contact;
use BYDAuto\Models\Testdrive;
use BYDAuto\Notifications\TestdirveNotification;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;

class BYDAutoCommand extends Command
{
    protected $signature = 'bydauto {action?}';

    protected $description = 'BYDAuto Command';

    public function handle()
    {
        $action = $this->argument('action');
        $method = "action_{$action}";
        if (method_exists($this, $method)) {
            $this->{$method}();
        } else {
            $this->error("不支持的操作 {$action}");
        }
    }

    public function action_mail_test()
    {
        $users = Contact::query()->get();
        foreach ($users as $user) {
            $message = new HelloMail(name: $user->remark);
            Mail::to($user->email)->send($message);
        }
    }

    public function action_init()
    {
        $this->call('db:wipe');
        $this->call('migrate');
    }

    // 同步试驾数据给到经销商联系人 按顺序每个联系人分配一个试驾数据
    public function action_push2contact()
    {
        // 所有已发送的试驾数据
        $sendItems = Testdrive::query()
            ->where('status', Testdrive::STATUS_PUBLISHED)
            ->pluck('mobile')
            ->toArray();

        // 需要发送的试驾数据
        $items = Testdrive::query()
            ->whereNotNull('source')
            ->where('status', Testdrive::STATUS_DRAFT)
            ->orderBy('id')
            ->get();

        // 根据手机号码去除重
        $items = $items->filter(function ($item) use ($sendItems) {
            return !in_array($item->mobile, $sendItems);
        });

        // 联系人
        $contacts = Contact::query()
            ->where('status', Contact::STATUS_PUBLISHED)
            ->orderBy('id')
            ->get();

        $count = $contacts->count();
        $offset = cache()->get('push2contact:offset', 0);

        $sendList = [];

        // 分配数据
        foreach ($items as $item) {
            if (mb_strlen($item->name) > 6) {
                info("试驾数据 {$item->id} 姓名异常 {$item->name}");
                $this->info("试驾数据 {$item->id} 姓名异常 {$item->name}");
                $item->update(['status' => Testdrive::STATUS_REJECTED]);
                continue;
            }
            $item->update(['status' => Testdrive::STATUS_PUBLISHED]);
            $contact = $contacts[$offset];
            if (!isset($sendList[$contact->id])) {
                $sendList[$contact->id] = [$item];
            } else {
                $sendList[$contact->id][] = $item;
            }
            // info("分配试驾数据 {$item->id} 给联系人 {$contact->id}");
            // $this->info("分配试驾数据 {$item->id} 给联系人 {$contact->id}");
            $offset = ($offset + 1) % $count;
        }
        cache()->forever('push2contact:offset', $offset);

        // 开始发送
        foreach ($sendList as $contactId => $items) {
            $contact = $contacts->firstWhere('id', $contactId);
            $this->info("发送试驾数据给联系人 {$contact->id} 总数 " . count($items));
            $contact->notify(new TestdirveNotification($items));
        }
        return Command::SUCCESS;
    }

    public function action_testdrive()
    {
        $items = Testdrive::query()->orderBy('id', 'desc')->get();
        $this->table(['id', 'date_created', 'status', 'name', 'mobile'], $items->map(function (Testdrive $v) {
            return [$v->id, $v->date_created, $v->status, $v->name, $v->mobile];
        }));
    }

    public function action_contact()
    {
        $items = Contact::all();
        $this->table(['id', 'email', 'remark', 'status'], $items->map(function (Contact $v) {
            return [$v->id, $v->email, $v->remark, $v->status];
        }));
    }

    public function action_contact_add()
    {
        $data['email'] = $this->ask('Email');
        $data['remark'] = $this->ask('Remark');
        $data['status'] = Contact::STATUS_PUBLISHED;

        $contact = new Contact($data);
        $contact->save();

        $this->info('Contact added');
    }
}
