<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\WorkingArea;
use App\Models\ServicePoint;

class WorkingAreaSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // فحص إذا كانت البيانات موجودة بالفعل
        if (WorkingArea::where('city_name_ar', 'الناصرية')->exists()) {
            $this->command->info('Working area الناصرية already exists, skipping...');
            return;
        }

        // إنشاء منطقة عمل الناصرية
        $workingArea = WorkingArea::create([
            'city_name' => 'Nassiriya',
            'city_name_ar' => 'الناصرية',
            'province' => 'Dhi Qar',
            'province_ar' => 'ذي قار',
            'center_lat' => 31.0437,
            'center_lng' => 46.2575,
            'zoom_level' => 12,
            'boundaries' => json_encode([
                'north' => 31.1,
                'south' => 31.0,
                'east' => 46.3,
                'west' => 46.2,
            ]),
            'description' => 'مدينة الناصرية - عاصمة محافظة ذي قار',
            'is_active' => true,
        ]);

        $this->command->info('Working area created: ' . $workingArea->city_name_ar);

        // إنشاء نقاط خدمة في الناصرية
        $servicePoints = [
            [
                'name' => 'Nassiriya General Hospital',
                'name_ar' => 'مستشفى الناصرية العام',
                'type' => 'restaurant',
                'lat' => 31.9550,
                'lng' => 46.2580,
                'address' => 'شارع الكفاح، الناصرية',
                'service_radius' => 5,
                'working_area_id' => $workingArea->id,
            ],
            [
                'name' => 'Central Market',
                'name_ar' => 'السوق المركزي',
                'type' => 'restaurant',
                'lat' => 31.9520,
                'lng' => 46.2560,
                'address' => 'وسط الناصرية',
                'service_radius' => 3,
                'working_area_id' => $workingArea->id,
            ],
            [
                'name' => 'Gas Station Al-Rashid',
                'name_ar' => 'محطة بنزين الرشيد',
                'type' => 'gas',
                'lat' => 31.9500,
                'lng' => 46.2550,
                'address' => 'شارع الرشيد، الناصرية',
                'service_radius' => 2,
                'working_area_id' => $workingArea->id,
            ],
            [
                'name' => 'Restaurant Al-Najaf',
                'name_ar' => 'مطعم النجف',
                'type' => 'restaurant',
                'lat' => 31.9540,
                'lng' => 46.2570,
                'address' => 'شارع الكفاح، الناصرية',
                'service_radius' => 4,
                'working_area_id' => $workingArea->id,
            ],
            [
                'name' => 'Taxi Station',
                'name_ar' => 'محطة التاكسي',
                'type' => 'taxi',
                'lat' => 31.9530,
                'lng' => 46.2585,
                'address' => 'شارع الكفاح، الناصرية',
                'service_radius' => 6,
                'working_area_id' => $workingArea->id,
            ],
        ];

        foreach ($servicePoints as $pointData) {
            // فحص إذا كانت نقطة الخدمة موجودة بالفعل
            if (!ServicePoint::where('name_ar', $pointData['name_ar'])->exists()) {
                ServicePoint::create($pointData);
                $this->command->info('Service point created: ' . $pointData['name_ar']);
            } else {
                $this->command->info('Service point already exists: ' . $pointData['name_ar']);
            }
        }

        $this->command->info('Service points created successfully!');
    }
}
