<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Payment;
use App\Models\User;
use App\Models\Trip;
use App\Models\FoodOrder;
use App\Models\GasOrder;
use App\Models\AppSetting;

class PaymentSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // التأكد من وجود إعدادات التطبيق
        if (!AppSetting::exists()) {
            $this->call(AppSettingSeeder::class);
        }

        // إنشاء مدفوعات تجريبية للرحلات
        $this->createTripPayments();

        // إنشاء مدفوعات تجريبية لطلبات الطعام
        $this->createFoodOrderPayments();

        // إنشاء مدفوعات تجريبية لطلبات الغاز
        $this->createGasOrderPayments();
    }

    private function createTripPayments(): void
    {
        $trips = Trip::take(5)->get();
        $users = User::where('user_type', 'passenger')->take(3)->get();
        $drivers = User::where('user_type', 'driver')->take(2)->get();

        foreach ($trips as $index => $trip) {
            $user = $users[$index % $users->count()];
            $driver = $drivers[$index % $drivers->count()];

            // تحديث الرحلة
            $trip->update([
                'passenger_id' => $user->passenger->id,
                'driver_id' => $driver->driver->id,
            ]);

            // إنشاء دفعة
            Payment::create([
                'user_id' => $user->id,
                'paymentable_type' => 'App\\Models\\Trip',
                'paymentable_id' => $trip->id,
                'amount' => rand(20, 100),
                'subscription_fee' => rand(2, 10),
                'total_amount' => rand(25, 110),
                'payment_method' => $this->getRandomPaymentMethod(),
                'status' => $this->getRandomStatus(),
                'transaction_id' => 'TXN' . strtoupper(uniqid()),
                'notes' => 'دفعة رحلة تجريبية',
                'created_at' => now()->subDays(rand(1, 30)),
            ]);
        }
    }

    private function createFoodOrderPayments(): void
    {
        $foodOrders = FoodOrder::take(3)->get();
        $users = User::where('user_type', 'passenger')->take(2)->get();

        foreach ($foodOrders as $index => $order) {
            $user = $users[$index % $users->count()];

            // تحديث طلب الطعام
            $order->update([
                'customer_id' => $user->id,
            ]);

            // إنشاء دفعة
            Payment::create([
                'user_id' => $user->id,
                'paymentable_type' => 'App\\Models\\FoodOrder',
                'paymentable_id' => $order->id,
                'amount' => rand(30, 150),
                'subscription_fee' => rand(3, 15),
                'total_amount' => rand(35, 165),
                'payment_method' => $this->getRandomPaymentMethod(),
                'status' => $this->getRandomStatus(),
                'transaction_id' => 'TXN' . strtoupper(uniqid()),
                'notes' => 'دفعة طلب طعام تجريبي',
                'created_at' => now()->subDays(rand(1, 20)),
            ]);
        }
    }

    private function createGasOrderPayments(): void
    {
        $gasOrders = GasOrder::take(3)->get();
        $users = User::where('user_type', 'passenger')->take(2)->get();

        foreach ($gasOrders as $index => $order) {
            $user = $users[$index % $users->count()];

            // تحديث طلب الغاز
            $order->update([
                'customer_id' => $user->id,
            ]);

            // إنشاء دفعة
            Payment::create([
                'user_id' => $user->id,
                'paymentable_type' => 'App\\Models\\GasOrder',
                'paymentable_id' => $order->id,
                'amount' => rand(50, 200),
                'subscription_fee' => rand(5, 20),
                'total_amount' => rand(55, 220),
                'payment_method' => $this->getRandomPaymentMethod(),
                'status' => $this->getRandomStatus(),
                'transaction_id' => 'TXN' . strtoupper(uniqid()),
                'notes' => 'دفعة طلب غاز تجريبي',
                'created_at' => now()->subDays(rand(1, 15)),
            ]);
        }
    }

    private function getRandomPaymentMethod(): string
    {
        $methods = ['cash', 'card', 'online', 'wallet'];
        return $methods[array_rand($methods)];
    }

    private function getRandomStatus(): string
    {
        $statuses = ['pending', 'completed', 'cancelled'];
        return $statuses[array_rand($statuses)];
    }
}
