<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\Producer;
use App\Models\User;
use Illuminate\Database\Seeder;
use Mortezaa97\Regions\Models\County;
use Mortezaa97\Regions\Models\Province;

class ProducersTableSeeder extends Seeder
{
    public function run(): void
    {
        $adminId = User::role('admin')->first()?->id;
        $user = User::role('user')->first();

        if (! $user) {
            return;
        }

        $province = Province::first();
        $county = County::where('province_id', $province?->id)->first();

        Producer::firstOrCreate(
            ['user_id' => $user->id],
            [
                'brand_name' => 'گاوداری نمونه',
                'work_email' => 'producer@example.com',
                'work_phone' => '021-12345678',
                'activity_main_category' => 'دامپروری',
                'activity_sub_category' => 'پرورش گاو شیری',
                'activity_product' => 'شیر و فرآورده‌های لبنی',
                'unit_capacity' => 100,
                'unit_area' => 5000,
                'work_province_id' => $province?->id,
                'work_county_id' => $county?->id,
                'work_address' => 'کیلومتر ۵ جاده قدیم',
                'work_postal_code' => '1234567890',
                'work_latitude' => 35.6892,
                'work_longitude' => 51.3890,
                'business_license' => json_encode(['number' => '12345', 'expiry' => now()->addYear()->format('Y-m-d')]),
                'education_certificates' => json_encode([]),
                'skill_certificates' => json_encode([]),
                'work_experience' => json_encode([]),
                'created_by' => $adminId,
                'updated_by' => $adminId,
            ]
        );
    }
}
