<?php

namespace Database\Seeders;

use App\Models\Account;
use App\Models\Branch;
use App\Models\PaymentMethod;
use Illuminate\Database\Seeder;

class PaymentMethodSeeder extends Seeder
{
    public function run(): void
    {
        // [name, channel, maps-to cash_bank account]
        $defaults = [
            ['Cash', 'cash', 'cash'],
            ['Bank Transfer', 'bank', 'bank'],
            ['Easypaisa', 'easypaisa', 'bank'],
            ['Jazz Cash', 'jazzcash', 'bank'],
            ['Cheque', 'cheque', 'bank'],
        ];

        foreach (Branch::all() as $branch) {
            $cash = Account::withoutGlobalScopes()->where('branch_id', $branch->id)->where('cash_bank', 'cash')->first();
            $bank = Account::withoutGlobalScopes()->where('branch_id', $branch->id)->where('cash_bank', 'bank')->first();

            $order = 0;
            foreach ($defaults as [$name, $channel, $cashBank]) {
                PaymentMethod::firstOrCreate(
                    ['branch_id' => $branch->id, 'channel' => $channel],
                    [
                        'name' => $name,
                        'account_id' => $cashBank === 'cash' ? $cash?->id : $bank?->id,
                        'is_active' => true,
                        'sort_order' => $order++,
                    ],
                );
            }
        }
    }
}
