<?php

namespace Database\Seeders;

use App\Models\KycDocument;
use App\Models\User;
use App\Models\UserPaymentMethod;
use App\Models\Wallet;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class QaSecondLinerSeeder extends Seeder
{
    public function run(): void
    {
        $email = (string) env('QA_LINER_EMAIL', 'liner2@lineup.fr');
        $password = (string) env('QA_LINER_PASSWORD', 'Lineup12j');
        $name = (string) env('QA_LINER_NAME', 'Liner Demo 2');
        $phone = (string) env('QA_LINER_PHONE', '+33600000003');
        $iban = (string) env('QA_LINER_IBAN', 'FR7630006000011234567890189');
        $bic = (string) env('QA_LINER_BIC', 'AGRIFRPP');
        $documentNumber = (string) env('QA_LINER_DOCUMENT_NUMBER', 'LNR-DEMO-002');
        $documentUrl = (string) env('QA_LINER_DOCUMENT_URL', 'https://example.com/kyc/liner2-cni.jpg');

        $liner = User::updateOrCreate(
            ['email' => strtolower($email)],
            [
                'name' => $name,
                'phone' => $phone,
                'password' => Hash::make($password),
                'role' => 'liner',
                'email_verified_at' => now(),
                'is_kyc_verified' => true,
                'is_available' => true,
                'email_notifications_enabled' => true,
                'language' => 'fr',
                'region' => 'FR',
                'push_notifications_enabled' => true,
                'sms_notifications_enabled' => false,
                'email_marketing_enabled' => true,
            ]
        );

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

        KycDocument::updateOrCreate(
            [
                'user_id' => $liner->id,
                'document_number' => $documentNumber,
            ],
            [
                'document_type' => 'cni',
                'document_url' => $documentUrl,
                'status' => 'approved',
                'notes' => 'Seeder QA: deuxieme liner de test multi-candidature.',
                'validated_at' => now(),
            ]
        );

        UserPaymentMethod::updateOrCreate(
            [
                'user_id' => $liner->id,
                'type' => UserPaymentMethod::TYPE_PAY_OUT,
                'provider' => 'rib',
            ],
            [
                'label' => 'RIB principal QA',
                'account_holder' => $name,
                'details' => [
                    'iban' => $iban,
                    'bic' => $bic,
                ],
                'is_default' => true,
                'is_active' => true,
                'verified_at' => now(),
            ]
        );

        UserPaymentMethod::query()
            ->where('user_id', $liner->id)
            ->where('type', UserPaymentMethod::TYPE_PAY_OUT)
            ->where('provider', '!=', 'rib')
            ->update(['is_default' => false]);

        $this->command?->info('QA liner ready: ' . $liner->email . ' / ' . $password);
    }
}
