<?php

namespace Database\Seeders;

use App\Models\CustomerBalance;
use App\Models\CustomerBalanceTransaction;
use App\Models\ReceiptVoucher;
use App\Models\PaymentVoucher;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class CustomerBalanceTransactionSeeder extends Seeder
{
    public function run(): void
    {
        $users = User::all();

        // إنشاء معاملات لسندات القبض
        $receipts = ReceiptVoucher::with('customer')->get();

        foreach ($receipts as $receipt) {
            $balance = CustomerBalance::where('customer_id', $receipt->customer_id)->first();

            if ($balance) {
                // حساب الرصيد السابق
                $previousBalance = DB::table('customer_balance_transactions')
                    ->where('customer_id', $receipt->customer_id)
                    ->orderBy('created_at', 'desc')
                    ->value('new_balance') ?? 0;

                $transactionType = $receipt->status == 'refunded' ? 'refund' : ($receipt->type == 'subscription' ? 'payment' : 'adjustment');

                // المبلغ يؤثر حسب نوع المعاملة
                $amount = $receipt->amount;
                $newBalance = $previousBalance;

                if ($transactionType == 'payment') {
                    $newBalance = $previousBalance + $amount;
                } elseif ($transactionType == 'refund') {
                    $newBalance = $previousBalance - $amount;
                }

                CustomerBalanceTransaction::create([
                    'balance_id' => $balance->id,
                    'customer_id' => $receipt->customer_id,
                    'transaction_type' => $transactionType,
                    'amount' => $amount,
                    'previous_balance' => $previousBalance,
                    'new_balance' => $newBalance,
                    'reference_id' => $receipt->id,
                    'description' => $receipt->notes ?? ($transactionType == 'payment' ? 'سند قبض' : 'مردود'),
                    'created_by' => $receipt->created_by ?? $users->random()->id,
                    'created_at' => $receipt->payment_date,
                ]);
            }
        }

        // إنشاء معاملات لسندات الصرف
        $payments = PaymentVoucher::with('customer')->where('status', 'active')->get();

        foreach ($payments as $payment) {
            $balance = CustomerBalance::where('customer_id', $payment->customer_id)->first();

            if ($balance) {
                $previousBalance = DB::table('customer_balance_transactions')
                    ->where('customer_id', $payment->customer_id)
                    ->orderBy('created_at', 'desc')
                    ->value('new_balance') ?? 0;

                CustomerBalanceTransaction::create([
                    'balance_id' => $balance->id,
                    'customer_id' => $payment->customer_id,
                    'transaction_type' => 'expense',
                    'amount' => $payment->amount,
                    'previous_balance' => $previousBalance,
                    'new_balance' => $previousBalance - $payment->amount,
                    'reference_id' => $payment->id,
                    'description' => $payment->notes ?? 'سند صرف - ' . $payment->expense_category,
                    'created_by' => $payment->created_by ?? $users->random()->id,
                    'created_at' => $payment->payment_date,
                ]);
            }
        }
    }
}
