<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Persist the insurance share breakdown on patient sessions so the payment page,
 * the invoice modal and the claims dashboard all read the same numbers instead of
 * each deriving their own.
 */
final class Version20260718184932 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Add insurance share breakdown columns to patient_sessions';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('ALTER TABLE patient_sessions '
            . 'ADD gross_total_rials INT DEFAULT 0 NOT NULL, '
            . 'ADD base_insurance_rials INT DEFAULT 0 NOT NULL, '
            . 'ADD supplementary_insurance_rials INT DEFAULT 0 NOT NULL, '
            . 'ADD patient_share_rials INT DEFAULT 0 NOT NULL');

        // Existing sessions were computed without an insurance breakdown: the whole
        // amount was the patient's share. Backfilling this way keeps their totals unchanged.
        $this->addSql('UPDATE patient_sessions SET '
            . 'patient_share_rials = final_price_rials, '
            . 'gross_total_rials = final_price_rials, '
            . 'base_insurance_rials = 0, '
            . 'supplementary_insurance_rials = 0');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('ALTER TABLE patient_sessions '
            . 'DROP gross_total_rials, '
            . 'DROP base_insurance_rials, '
            . 'DROP supplementary_insurance_rials, '
            . 'DROP patient_share_rials');
    }
}
