<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\BankCard;
use App\Models\Expert;
use App\Models\Producer;
use Database\Seeders\Support\DemoSeederContext;
use Illuminate\Database\Seeder;

class DemoAccountsSeeder extends Seeder
{
    public function run(): void
    {
        $admin = DemoSeederContext::admin();
        $employer = DemoSeederContext::employer();
        $producerUser = DemoSeederContext::producerUser();
        $expertUser = DemoSeederContext::expertUser();
        $province = DemoSeederContext::province();
        $county = DemoSeederContext::county($province);

        if (! $admin || ! $producerUser || ! $expertUser) {
            return;
        }

        $this->reassignProducerProfile($employer, $producerUser);

        Producer::updateOrCreate(
            ['user_id' => $producerUser->id],
            [
                'brand_name' => 'گاوداری نمونه ریوو',
                'work_email' => 'producer@rievo.ir',
                'work_phone' => '021-12345678',
                'activity_main_category' => 'دامپروری',
                'activity_sub_category' => 'پرورش گاو شیری',
                'activity_product' => 'شیر و فرآورده‌های لبنی',
                'unit_capacity' => 120,
                'unit_area' => 5500,
                'work_province_id' => $province?->id,
                'work_county_id' => $county?->id,
                'work_address' => 'کیلومتر ۵ جاده قدیم کرج',
                'work_postal_code' => '1234567890',
                'work_latitude' => 35.6892,
                'work_longitude' => 51.3890,
                'status' => Producer::STATUS_APPROVED,
                'reviewed_by' => $admin->id,
                'reviewed_at' => now()->subMonths(2),
                'business_license' => ['number' => '12345', 'expiry' => now()->addYear()->format('Y-m-d')],
                'education_certificates' => [],
                'skill_certificates' => [],
                'work_experience' => [],
                'created_by' => $admin->id,
                'updated_by' => $admin->id,
            ]
        );

        Producer::updateOrCreate(
            ['brand_name' => 'واحد مرغداری در انتظار تایید'],
            [
                'user_id' => $producerUser->id,
                'work_email' => 'pending-producer@rievo.ir',
                'work_phone' => '021-55555555',
                'activity_main_category' => 'مرغداری',
                'status' => Producer::STATUS_PENDING,
                'work_province_id' => $province?->id,
                'work_county_id' => $county?->id,
                'work_address' => 'تهران، واحد در انتظار تایید',
                'work_latitude' => 35.6892,
                'work_longitude' => 51.3890,
                'business_license' => ['number' => 'PENDING-001'],
                'education_certificates' => [],
                'skill_certificates' => [],
                'work_experience' => [],
                'created_by' => $admin->id,
                'updated_by' => $admin->id,
            ]
        );

        Expert::updateOrCreate(
            ['user_id' => $expertUser->id],
            [
                'brand_name' => 'کلینیک دامپزشکی نمونه',
                'work_email' => 'expert@rievo.ir',
                'work_phone' => '021-87654321',
                'main_category' => 'دامپزشکی',
                'sub_category' => 'دام بزرگ',
                'species_type' => 'گاو، گوسفند، بز',
                'work_province_id' => $province?->id,
                'work_county_id' => $county?->id,
                'work_address' => 'خیابان اصلی، پلاک ۱۰',
                'work_latitude' => 35.6892,
                'work_longitude' => 51.3890,
                'status' => Expert::STATUS_APPROVED,
                'reviewed_by' => $admin->id,
                'reviewed_at' => now()->subMonths(3),
                'created_by' => $admin->id,
                'updated_by' => $admin->id,
            ]
        );

        if ($employer) {
            BankCard::firstOrCreate(
                [
                    'user_id' => $employer->id,
                    'shaba' => 'IR120170000000123456789012',
                ],
                [
                    'bank_name' => 'ملت',
                    'card_number' => '6104330000001234',
                    'account_holder_name' => 'یوسف روشندل',
                    'is_default' => true,
                    'status' => BankCard::STATUS_ACTIVE,
                ]
            );
        }

        BankCard::firstOrCreate(
            [
                'user_id' => $producerUser->id,
                'shaba' => 'IR450560000000987654321098',
            ],
            [
                'bank_name' => 'سامان',
                'card_number' => '6219860000005678',
                'account_holder_name' => 'رضا دامدار',
                'is_default' => true,
                'status' => BankCard::STATUS_ACTIVE,
            ]
        );
    }

    private function reassignProducerProfile(?\App\Models\User $employer, \App\Models\User $producerUser): void
    {
        if (! $employer || $employer->id === $producerUser->id) {
            return;
        }

        Producer::query()
            ->where('user_id', $employer->id)
            ->update(['user_id' => $producerUser->id]);
    }
}
