<?php

namespace Database\Seeders;

use App\Models\User;
use App\Models\DryCleaner;
use App\Models\Rider;
use App\Models\Order;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $testCustomerEmail = env('TEST_CUSTOMER_EMAIL', 'test@test.com');
        $testCustomerOtp = (string) env('TEST_CUSTOMER_OTP', '12345');

        // ── Owners ───────────────────────────────────────
        $owner1 = User::create([
            'name' => 'James Mwangi',
            'email' => 'owner@freshpress.co.ke',
            'password' => Hash::make('password'),
            'phone' => '0712000001',
            'role' => 'owner',
        ]);

        $owner2 = User::create([
            'name' => 'Amina Hassan',
            'email' => 'amina@freshpress.co.ke',
            'password' => Hash::make('password'),
            'phone' => '0712000002',
            'role' => 'owner',
        ]);

        // ── Dry Cleaners ────────────────────────────────
        $dc1 = DryCleaner::create([
            'owner_id' => $owner1->id,
            'name' => 'SparkleClean Westlands',
            'description' => 'Premium dry cleaning in the heart of Westlands. Same-day service available. We handle suits, dresses, and delicate fabrics with care.',
            'price_per_kg' => 250,
            'latitude' => -1.2674,
            'longitude' => 36.8116,
            'address' => 'Westlands Rd, Nairobi',
            'phone' => '0712100001',
            'rating' => 4.6,
            'total_ratings' => 32,
            'status' => 'active',
        ]);

        $dc2 = DryCleaner::create([
            'owner_id' => $owner1->id,
            'name' => 'FreshPress CBD',
            'description' => 'Fast and affordable dry cleaning in Nairobi CBD. Drop off before 9 AM, pick up by 5 PM.',
            'price_per_kg' => 180,
            'latitude' => -1.2864,
            'longitude' => 36.8172,
            'address' => 'Kenyatta Ave, Nairobi CBD',
            'phone' => '0712100002',
            'rating' => 4.2,
            'total_ratings' => 58,
            'status' => 'active',
        ]);

        $dc3 = DryCleaner::create([
            'owner_id' => $owner2->id,
            'name' => 'CleanWave Kilimani',
            'description' => 'Eco-friendly dry cleaning using organic solvents. Special rates for bulk orders. Free pickup & delivery within 3 km.',
            'price_per_kg' => 300,
            'latitude' => -1.2921,
            'longitude' => 36.7875,
            'address' => 'Argwings Kodhek Rd, Kilimani',
            'phone' => '0712100003',
            'rating' => 4.8,
            'total_ratings' => 21,
            'status' => 'active',
        ]);

        $dc4 = DryCleaner::create([
            'owner_id' => $owner2->id,
            'name' => 'QuickWash Karen',
            'description' => 'Serving the Karen community with top-quality laundry and dry cleaning services since 2018.',
            'price_per_kg' => 220,
            'latitude' => -1.3184,
            'longitude' => 36.7113,
            'address' => 'Karen Rd, Nairobi',
            'phone' => '0712100004',
            'rating' => 4.4,
            'total_ratings' => 15,
            'status' => 'active',
        ]);

        $dc5 = DryCleaner::create([
            'owner_id' => $owner1->id,
            'name' => 'PristinePress South B',
            'description' => 'Budget-friendly dry cleaning without compromising quality. Student discounts available.',
            'price_per_kg' => 150,
            'latitude' => -1.3106,
            'longitude' => 36.8380,
            'address' => 'Mombasa Rd, South B',
            'phone' => '0712100005',
            'rating' => 4.0,
            'total_ratings' => 44,
            'status' => 'active',
        ]);

        // ── Rider users ─────────────────────────────────
        $riderUser1 = User::create([
            'name' => 'Kevin Ochieng',
            'email' => 'rider@freshpress.co.ke',
            'password' => Hash::make('password'),
            'phone' => '0712200001',
            'role' => 'rider',
        ]);

        $riderUser2 = User::create([
            'name' => 'Brian Kipchoge',
            'email' => 'brian@freshpress.co.ke',
            'password' => Hash::make('password'),
            'phone' => '0712200002',
            'role' => 'rider',
        ]);

        $riderUser3 = User::create([
            'name' => 'Samuel Wanjiku',
            'email' => 'samuel@freshpress.co.ke',
            'password' => Hash::make('password'),
            'phone' => '0712200003',
            'role' => 'rider',
        ]);

        $riderUser4 = User::create([
            'name' => 'Peter Njoro',
            'email' => 'peter@freshpress.co.ke',
            'password' => Hash::make('password'),
            'phone' => '0712200004',
            'role' => 'rider',
        ]);

        // ── Riders (assigned to DCs) ────────────────────
        Rider::create([
            'user_id' => $riderUser1->id,
            'dry_cleaner_id' => $dc1->id,
            'id_number' => '30001001',
            'motorbike_plates' => 'KMFA 001A',
            'license_number' => 'DL-001-2024',
            'status' => 'available',
            'current_lat' => -1.2680,
            'current_lng' => 36.8120,
        ]);

        Rider::create([
            'user_id' => $riderUser2->id,
            'dry_cleaner_id' => $dc2->id,
            'id_number' => '30002002',
            'motorbike_plates' => 'KMFB 002B',
            'license_number' => 'DL-002-2024',
            'status' => 'available',
            'current_lat' => -1.2870,
            'current_lng' => 36.8180,
        ]);

        Rider::create([
            'user_id' => $riderUser3->id,
            'dry_cleaner_id' => $dc3->id,
            'id_number' => '30003003',
            'motorbike_plates' => 'KMFC 003C',
            'license_number' => 'DL-003-2024',
            'status' => 'available',
            'current_lat' => -1.2925,
            'current_lng' => 36.7880,
        ]);

        Rider::create([
            'user_id' => $riderUser4->id,
            'dry_cleaner_id' => $dc1->id,
            'id_number' => '30004004',
            'motorbike_plates' => 'KMFD 004D',
            'license_number' => 'DL-004-2024',
            'status' => 'offline',
            'current_lat' => -1.2700,
            'current_lng' => 36.8100,
        ]);

        // ── Sample customer ─────────────────────────────
        $customer = User::create([
            'name' => 'Test Customer',
            'email' => $testCustomerEmail,
            'password' => Hash::make('password'),
            'phone' => '0712300001',
            'role' => 'customer',
        ]);

        // ── Hardcoded OTP for test customer ──────────────
        DB::table('email_otps')->insert([
            'email' => $testCustomerEmail,
            'code' => $testCustomerOtp,
            'expires_at' => Carbon::now()->addYears(10),
            'used' => false,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        // ── Test Orders (low amounts for live M-Pesa testing) ──
        // Order 1: Pending payment
        Order::create([
            'customer_id' => $customer->id,
            'dry_cleaner_id' => $dc1->id,
            'pickup_rider_id' => 1,
            'status' => 'washed',
            'weight_kg' => 0.02,
            'total_amount' => 5.00,
            'weight_confirmed' => true,
            'pickup_lat' => -1.2700,
            'pickup_lng' => 36.8130,
            'pickup_address' => 'Sarit Centre, Westlands',
            'notes' => 'Pending payment test order',
        ]);

        // Order 2: Pending payment
        Order::create([
            'customer_id' => $customer->id,
            'dry_cleaner_id' => $dc2->id,
            'pickup_rider_id' => 2,
            'status' => 'washed',
            'weight_kg' => 0.03,
            'total_amount' => 5.00,
            'weight_confirmed' => true,
            'pickup_lat' => -1.2860,
            'pickup_lng' => 36.8175,
            'pickup_address' => 'Kencom House, CBD',
            'notes' => 'Pending payment test order',
        ]);

        // Order 3: Pending payment
        Order::create([
            'customer_id' => $customer->id,
            'dry_cleaner_id' => $dc3->id,
            'status' => 'washed',
            'weight_kg' => 0.03,
            'total_amount' => 5.00,
            'weight_confirmed' => true,
            'pickup_lat' => -1.2930,
            'pickup_lng' => 36.7880,
            'pickup_address' => 'Yaya Centre, Kilimani',
            'notes' => 'Pending payment test order',
        ]);

        // Order 4: Pending payment
        Order::create([
            'customer_id' => $customer->id,
            'dry_cleaner_id' => $dc5->id,
            'status' => 'washed',
            'weight_kg' => 0.02,
            'total_amount' => 4.00,
            'weight_confirmed' => true,
            'pickup_lat' => -1.3100,
            'pickup_lng' => 36.8370,
            'pickup_address' => 'South B Shopping Centre',
            'notes' => 'Pending payment test order',
        ]);

        // Order 5: Pending payment
        Order::create([
            'customer_id' => $customer->id,
            'dry_cleaner_id' => $dc1->id,
            'pickup_rider_id' => 1,
            'status' => 'washed',
            'weight_kg' => 0.02,
            'total_amount' => 3.00,
            'weight_confirmed' => true,
            'pickup_lat' => -1.2685,
            'pickup_lng' => 36.8110,
            'pickup_address' => 'Westgate Mall, Westlands',
            'notes' => 'Pending payment test order',
        ]);

        $this->command->info('');
        $this->command->info('╔══════════════════════════════════════════════╗');
        $this->command->info('║        SEEDED TEST ACCOUNTS                 ║');
        $this->command->info('╠══════════════════════════════════════════════╣');
        $this->command->info('║  Owner:  owner@freshpress.co.ke / password  ║');
        $this->command->info('║  Owner:  amina@freshpress.co.ke / password  ║');
        $this->command->info('║  Rider:  rider@freshpress.co.ke / password  ║');
        $this->command->info('║  Rider:  brian@freshpress.co.ke / password  ║');
        $this->command->info("║  Customer: {$testCustomerEmail} / OTP: {$testCustomerOtp}      ║");
        $this->command->info('║  5 Dry Cleaners seeded across Nairobi       ║');
        $this->command->info('║  4 Riders assigned to DCs                   ║');
        $this->command->info('║  5 Test Orders (KSh 5 each for M-Pesa)     ║');
        $this->command->info('╚══════════════════════════════════════════════╝');
    }
}
