<?php

namespace Database\Seeders;

use App\Models\Mission;
use App\Models\MissionApplication;
use App\Models\MissionDispute;
use App\Models\MissionProof;
use App\Models\Payment;
use App\Models\KycDocument;
use App\Models\PushDeviceToken;
use App\Models\UserNotification;
use App\Models\UserPaymentMethod;
use App\Models\User;
use App\Models\Wallet;
use App\Models\WalletTransaction;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    use WithoutModelEvents;

    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $admin = User::updateOrCreate(
            ['email' => 'admin@lineup.fr'],
            [
                'name' => 'LineUp Admin',
                'password' => Hash::make('Lineup12j'),
                'role' => 'admin',
                'email_verified_at' => now(),
            ]
        );

        $client = User::updateOrCreate(
            ['email' => 'client@lineup.fr'],
            [
                'name' => 'Client Demo',
                'password' => Hash::make('Lineup12j'),
                'role' => 'client',
                'phone' => '+33600000001',
                'email_verified_at' => now(),
                'is_kyc_verified' => true,
            ]
        );

        $liner = User::updateOrCreate(
            ['email' => 'liner@lineup.fr'],
            [
                'name' => 'Liner Demo',
                'password' => Hash::make('Lineup12j'),
                'role' => 'liner',
                'phone' => '+33600000002',
                'email_verified_at' => now(),
                'is_kyc_verified' => true,
            ]
        );

        $mission = Mission::updateOrCreate(
            [
                'client_id' => $client->id,
                'title' => 'Concert Zenith - file standard',
            ],
            [
                'liner_id' => $liner->id,
                'description' => 'Arriver 2h avant ouverture et garder la place.',
                'event_name' => 'Concert Zenith',
                'location' => 'Paris 19e',
                'expected_start_at' => now()->addDay(),
                'status' => 'completed',
                'budget_amount' => 55,
                'currency' => 'EUR',
                'accepted_at' => now()->subHours(5),
                'started_at' => now()->subHours(4),
                'arrived_at' => now()->subHours(3),
                'completed_at' => now()->subHours(2),
            ]
        );

        MissionApplication::updateOrCreate(
            [
                'mission_id' => $mission->id,
                'liner_id' => $liner->id,
            ],
            [
                'status' => 'accepted',
                'accepted_at' => $mission->accepted_at ?? now()->subHours(5),
                'rejected_at' => null,
            ]
        );

        $clientPayInMethod = UserPaymentMethod::updateOrCreate(
            [
                'user_id' => $client->id,
                'type' => UserPaymentMethod::TYPE_PAY_IN,
                'provider' => 'stripe',
            ],
            [
                'label' => 'Carte principale Stripe',
                'account_holder' => $client->name,
                'details' => [
                    'customer_id' => 'cus_demo_client_001',
                    'payment_method_id' => 'pm_demo_001',
                ],
                'is_default' => true,
                'is_active' => true,
            ]
        );

        UserPaymentMethod::updateOrCreate(
            [
                'user_id' => $liner->id,
                'type' => UserPaymentMethod::TYPE_PAY_OUT,
                'provider' => 'paypal',
            ],
            [
                'label' => 'PayPal principal',
                'account_holder' => $liner->name,
                'details' => [
                    'email' => 'liner-paypal@lineup.fr',
                ],
                'is_default' => true,
                'is_active' => true,
            ]
        );

        Payment::updateOrCreate(
            ['mission_id' => $mission->id],
            [
                'client_id' => $client->id,
                'liner_id' => $liner->id,
                'payment_method_id' => $clientPayInMethod->id,
                'amount' => 55,
                'currency' => 'EUR',
                'status' => 'paid',
                'provider' => 'stripe',
                'paid_at' => now()->subHours(2),
                'metadata' => [
                    'payment_method_id' => $clientPayInMethod->id,
                    'payment_method_label' => $clientPayInMethod->label,
                ],
            ]
        );

        $wallet = Wallet::firstOrCreate(
            ['user_id' => $liner->id],
            ['balance' => 55, 'currency' => 'EUR']
        );

        $wallet->update(['balance' => 55]);

        WalletTransaction::updateOrCreate(
            [
                'wallet_id' => $wallet->id,
                'reference_type' => Mission::class,
                'reference_id' => $mission->id,
            ],
            [
                'user_id' => $liner->id,
                'type' => 'credit',
                'amount' => 55,
                'description' => 'Mission demo settlement',
            ]
        );

        MissionProof::updateOrCreate(
            [
                'mission_id' => $mission->id,
                'liner_id' => $liner->id,
                'type' => 'photo',
            ],
            [
                'proof_url' => 'https://example.com/proofs/mission-demo.jpg',
                'status' => 'pending',
            ]
        );

        MissionDispute::updateOrCreate(
            [
                'mission_id' => $mission->id,
                'reporter_id' => $client->id,
                'category' => 'delai',
            ],
            [
                'against_user_id' => $liner->id,
                'description' => 'Le liner est arrive plus tard que prevu sur cette mission.',
                'status' => 'open',
            ]
        );

        KycDocument::updateOrCreate(
            [
                'user_id' => $liner->id,
                'document_number' => 'LNR-DEMO-001',
            ],
            [
                'document_type' => 'passport',
                'status' => 'pending',
                'notes' => 'Document en attente de moderation BO.',
            ]
        );

        KycDocument::updateOrCreate(
            [
                'user_id' => $liner->id,
                'document_number' => 'LNR-DEMO-000',
            ],
            [
                'document_type' => 'id_card',
                'status' => 'approved',
                'validated_by' => $admin->id,
                'validated_at' => now()->subDay(),
                'notes' => 'Validation precedente.',
            ]
        );

        UserNotification::updateOrCreate(
            [
                'user_id' => $client->id,
                'type' => 'mission.accepted',
                'title' => 'Mission acceptee',
            ],
            [
                'body' => 'Votre mission de demo est acceptee par un liner.',
                'payload' => [
                    'mission_id' => $mission->id,
                    'status' => 'accepted',
                ],
                'read_at' => null,
            ]
        );

        UserNotification::updateOrCreate(
            [
                'user_id' => $liner->id,
                'type' => 'payment.validated',
                'title' => 'Paiement valide',
            ],
            [
                'body' => 'Le paiement de la mission demo est valide.',
                'payload' => [
                    'mission_id' => $mission->id,
                    'amount' => 55,
                    'currency' => 'EUR',
                ],
                'read_at' => now()->subHour(),
            ]
        );

        PushDeviceToken::updateOrCreate(
            ['token' => 'demo-client-token-001'],
            [
                'user_id' => $client->id,
                'provider' => 'fcm',
                'platform' => 'android',
                'device_name' => 'Client Pixel Demo',
                'last_seen_at' => now()->subMinutes(5),
                'disabled_at' => null,
            ]
        );

        PushDeviceToken::updateOrCreate(
            ['token' => 'demo-liner-token-001'],
            [
                'user_id' => $liner->id,
                'provider' => 'fcm',
                'platform' => 'ios',
                'device_name' => 'Liner iPhone Demo',
                'last_seen_at' => now()->subMinutes(3),
                'disabled_at' => null,
            ]
        );
    }
}
