<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Central admin-managed coverage percentages per insurance and service category.
 */
final class Version20260725115603 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add insurance_coverage_defaults (admin-managed coverage percent per insurance and service category)';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE insurance_coverage_defaults (id INT AUTO_INCREMENT NOT NULL, insurance_id INT NOT NULL, service_category VARCHAR(30) NOT NULL, coverage_percent NUMERIC(5, 2) NOT NULL, updated_at INT NOT NULL, UNIQUE INDEX uniq_insurance_service_category (insurance_id, service_category), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');

        // Every insurance gets a full row set so the admin panel always shows a complete
        // grid: a missing row must never be read as "0% on purpose".
        foreach (['outpatient', 'inpatient'] as $category) {
            $this->addSql(
                'INSERT INTO insurance_coverage_defaults (insurance_id, service_category, coverage_percent, updated_at)
                 SELECT i.id, :category, 0.00, UNIX_TIMESTAMP() FROM insurances i',
                ['category' => $category],
            );
        }
    }

    public function down(Schema $schema): void
    {
        $this->addSql('DROP TABLE insurance_coverage_defaults');
    }
}
