<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    public function up(): void
    {
        $this->setCalculatorState(false);
    }

    public function down(): void
    {
        $this->setCalculatorState(true);
    }

    private function setCalculatorState(bool $enabled): void
    {
        $service = DB::table('services')->where('slug', 'photovoltaik')->first();

        if (! $service) {
            return;
        }

        $decision = json_decode($service->decision_content ?: '[]', true) ?: [];
        $decision['calculator_enabled'] = $enabled;

        DB::table('services')->where('id', $service->id)->update([
            'decision_content' => json_encode($decision, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
            'updated_at' => now(),
        ]);
    }
};
