<?php

namespace Database\Seeders;

use App\Models\User;
use App\Models\TailorSpecialization;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // Clear existing data
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');
        DB::table('customer_specialization')->truncate();
        DB::table('tailor_specialization_user')->truncate();
        DB::table('tailor_specializations')->truncate();
        DB::table('users')->truncate();
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');

        // Seed Tailor Specializations first
        $specializations = [
            ['name' => 'Baju Pria', 'category' => 'Pria', 'photo' => 'specializations/pria.jpg'],
            ['name' => 'Baju Wanita', 'category' => 'Wanita', 'photo' => 'specializations/wanita.jpg'],
            ['name' => 'Baju Anak', 'category' => 'Anak', 'photo' => 'specializations/anak.jpg'],
            ['name' => 'Baju Muslim', 'category' => 'Muslim', 'photo' => 'specializations/muslim.jpg'],
            ['name' => 'Baju Pesta', 'category' => 'Pesta', 'photo' => 'specializations/pesta.jpg'],
            ['name' => 'Baju Olahraga', 'category' => 'Olahraga', 'photo' => 'specializations/olahraga.jpg'],
        ];

        foreach ($specializations as $spec) {
            TailorSpecialization::create($spec);
        }

        // Seed verified pelanggan
        $pelanggan = [
            [
                'name' => 'Ujang',
                'email' => 'ahmdrifai290@gmail.com',
                'password' => Hash::make('pelanggan123'),
                'role' => 'pelanggan',
                'phone_number' => '081234567890',
                'address' => 'Jl. Merdeka No. 123, Jakarta',
                'latitude' => -6.2088,
                'longitude' => 106.8456,
                'email_verified_at' => Carbon::now(),
                'preferred_specializations' => [1, 2] // Baju Pria dan Wanita
            ],
            [
                'name' => 'Sarah Wijaya',
                'email' => 'sarah.wijaya@gmail.com',
                'password' => Hash::make('password123'),
                'role' => 'pelanggan',
                'phone_number' => '081234567891',
                'address' => 'Jl. Sudirman No. 45, Jakarta',
                'latitude' => -6.2088,
                'longitude' => 106.8456,
                'email_verified_at' => Carbon::now(),
                'preferred_specializations' => [2, 4] // Baju Wanita dan Muslim
            ],
            [
                'name' => 'Budi Santoso',
                'email' => 'budi.santoso@gmail.com',
                'password' => Hash::make('password123'),
                'role' => 'pelanggan',
                'phone_number' => '081234567892',
                'address' => 'Jl. Gatot Subroto No. 67, Jakarta',
                'latitude' => -6.2088,
                'longitude' => 106.8456,
                'email_verified_at' => Carbon::now(),
                'preferred_specializations' => [1, 5] // Baju Pria dan Pesta
            ]
        ];

        foreach ($pelanggan as $p) {
            $user = User::create([
                'name' => $p['name'],
                'email' => $p['email'],
                'password' => $p['password'],
                'role' => $p['role'],
                'phone_number' => $p['phone_number'],
                'address' => $p['address'],
                'latitude' => $p['latitude'],
                'longitude' => $p['longitude'],
                'email_verified_at' => $p['email_verified_at']
            ]);

            // Attach preferred specializations
            $user->preferredSpecializations()->attach($p['preferred_specializations']);
        }

        // Seed verified penjahit
        $penjahit = [
            [
                'name' => 'Asep Tailor',
                'email' => 'ahmdrifai2905@gmail.com',
                'password' => Hash::make('penjahit123'),
                'role' => 'penjahit',
                'phone_number' => '081234567893',
                'address' => 'Jl. Thamrin No. 89, Jakarta',
                'latitude' => -6.2088,
                'longitude' => 106.8456,
                'email_verified_at' => Carbon::now(),
                'shop_description' => 'Toko jahit profesional dengan pengalaman 10 tahun',
                'specializations' => [1, 2, 5] // Baju Pria, Wanita, dan Pesta
            ],
            [
                'name' => 'Maya Fashion',
                'email' => 'maya.fashion@gmail.com',
                'password' => Hash::make('password123'),
                'role' => 'penjahit',
                'phone_number' => '081234567894',
                'address' => 'Jl. Asia Afrika No. 34, Bandung',
                'latitude' => -6.9175,
                'longitude' => 107.6191,
                'email_verified_at' => Carbon::now(),
                'shop_description' => 'Spesialis jahit baju wanita dan muslim',
                'specializations' => [2, 4] // Baju Wanita dan Muslim
            ],
            [
                'name' => 'Rudi Custom Tailor',
                'email' => 'rudi.tailor@gmail.com',
                'password' => Hash::make('password123'),
                'role' => 'penjahit',
                'phone_number' => '081234567895',
                'address' => 'Jl. Veteran No. 56, Surabaya',
                'latitude' => -7.2575,
                'longitude' => 112.7521,
                'email_verified_at' => Carbon::now(),
                'shop_description' => 'Toko jahit custom untuk pria dan wanita',
                'specializations' => [1, 2, 3] // Baju Pria, Wanita, dan Anak
            ]
        ];

        foreach ($penjahit as $p) {
            $user = User::create([
                'name' => $p['name'],
                'email' => $p['email'],
                'password' => $p['password'],
                'role' => $p['role'],
                'phone_number' => $p['phone_number'],
                'address' => $p['address'],
                'latitude' => $p['latitude'],
                'longitude' => $p['longitude'],
                'email_verified_at' => $p['email_verified_at'],
                'shop_description' => $p['shop_description']
            ]);

            // Attach specializations
            $user->specializations()->attach($p['specializations']);
        }

        $this->call([
            AdminUserSeeder::class
        ]);
    }
}
