<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    public function up(): void
    {
        // Insert default profit calculation settings
        $settings = [
            [
                'key' => 'profit_include_shipping_in_cogs',
                'value' => 'true',
                'created_at' => now(),
                'updated_at' => now()
            ],
            [
                'key' => 'profit_include_discounts_in_cogs',
                'value' => 'true',
                'created_at' => now(),
                'updated_at' => now()
            ],
            [
                'key' => 'profit_include_tax_in_cogs',
                'value' => 'false',
                'created_at' => now(),
                'updated_at' => now()
            ],
            [
                'key' => 'profit_purchase_price_fallback_policy',
                'value' => 'mark_zero_and_flag',
                'created_at' => now(),
                'updated_at' => now()
            ]
        ];

        foreach ($settings as $setting) {
            DB::table('settings')->updateOrInsert(
                ['key' => $setting['key']],
                $setting
            );
        }
    }

    public function down(): void
    {
        DB::table('settings')->whereIn('key', [
            'profit_include_shipping_in_cogs',
            'profit_include_discounts_in_cogs',
            'profit_include_tax_in_cogs',
            'profit_purchase_price_fallback_policy'
        ])->delete();
    }
};