<?php

namespace Modules\PurchaseRequisition\Database\Seeders;

use Illuminate\Database\Seeder;
use Modules\PurchaseRequisition\Entities\PrCurrency;
use App\Business;

class DefaultCurrenciesSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Get all businesses
        $businesses = Business::all();

        foreach ($businesses as $business) {
            // Check if currencies already exist
            $existingCount = PrCurrency::where('business_id', $business->id)->count();

            if ($existingCount == 0) {
                // Create default currencies
                $currencies = [
                    [
                        'business_id' => $business->id,
                        'code' => 'EGP',
                        'name' => 'Egyptian Pound',
                        'symbol' => 'EGP',
                        'conversion_rate_to_egp' => 1.0000,
                        'is_active' => true,
                    ],
                    [
                        'business_id' => $business->id,
                        'code' => 'USD',
                        'name' => 'US Dollar',
                        'symbol' => '$',
                        'conversion_rate_to_egp' => 48.5000,
                        'is_active' => true,
                    ],
                    [
                        'business_id' => $business->id,
                        'code' => 'CNY',
                        'name' => 'Chinese Yuan',
                        'symbol' => '¥',
                        'conversion_rate_to_egp' => 6.7000,
                        'is_active' => true,
                    ],
                    [
                        'business_id' => $business->id,
                        'code' => 'EUR',
                        'name' => 'Euro',
                        'symbol' => '€',
                        'conversion_rate_to_egp' => 52.5000,
                        'is_active' => true,
                    ],
                ];

                foreach ($currencies as $currency) {
                    PrCurrency::create($currency);
                }

                $this->command->info("Created default currencies for business: {$business->name}");
            } else {
                $this->command->info("Currencies already exist for business: {$business->name}");
            }
        }

        $this->command->info('Default currencies seeding completed!');
    }
}
