<?php

/**
 * Migration 047 – Add Schema.org LocalBusiness & FAQ settings
 *
 * Adds address/geo fields for LocalBusiness JSON-LD and a toggle for FAQ schema.
 */

use App\Database\Migration;

class AddSchemaOrgSettings extends Migration
{
    public function up(): void
    {
        $settings = [
            ['local_business_enabled',  '0',  'seo'],
            ['local_business_type',     'LocalBusiness', 'seo'],
            ['local_business_street',   '',   'seo'],
            ['local_business_city',     '',   'seo'],
            ['local_business_postal',   '',   'seo'],
            ['local_business_country',  '',   'seo'],
            ['local_business_lat',      '',   'seo'],
            ['local_business_lng',      '',   'seo'],
            ['local_business_hours',    '',   'seo'],
            ['faq_schema_enabled',      '1',  'seo'],
        ];

        foreach ($settings as [$key, $value, $group]) {
            $exists = $this->db->fetch("SELECT 1 FROM iw_settings WHERE setting_key = :k", ['k' => $key]);
            if (!$exists) {
                $this->db->insert('iw_settings', [
                    'setting_key'   => $key,
                    'setting_value' => $value,
                    'setting_group' => $group,
                ]);
            }
        }
    }

    public function down(): void
    {
        $keys = [
            'local_business_enabled', 'local_business_type', 'local_business_street',
            'local_business_city', 'local_business_postal', 'local_business_country',
            'local_business_lat', 'local_business_lng', 'local_business_hours',
            'faq_schema_enabled',
        ];
        $placeholders = implode(',', array_fill(0, count($keys), '?'));
        $this->db->query("DELETE FROM iw_settings WHERE setting_key IN ({$placeholders})", $keys);
    }
}
