<?php

declare(strict_types=1);

namespace OCA\Mitgliederverwaltung\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/**
 * Migration: Create fee rules and fee records tables.
 *
 * Part of Issue #41 — DB migrations for fee rules and records.
 */
class Version000007Date20260407000006 extends SimpleMigrationStep {

    public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
        /** @var ISchemaWrapper $schema */
        $schema = $schemaClosure();
        $schemaChanged = false;

        // ── Fee rules table ──────────────────────────────────────────
        if (!$schema->hasTable('mv_fee_rules')) {
            $table = $schema->createTable('mv_fee_rules');

            $table->addColumn('id', Types::BIGINT, [
                'autoincrement' => true,
                'notnull' => true,
                'unsigned' => true,
            ]);
            $table->setPrimaryKey(['id']);

            $table->addColumn('year_from', Types::INTEGER, [
                'notnull' => true,
            ]);

            $table->addColumn('base_rate', Types::DECIMAL, [
                'notnull' => true,
                'precision' => 10,
                'scale' => 2,
            ]);

            $table->addColumn('family_rules_json', Types::TEXT, [
                'notnull' => true,
            ]);

            $table->addColumn('inactive_rule', Types::STRING, [
                'notnull' => true,
                'length' => 50,
                'default' => 'freeze_rate_at_exit',
            ]);

            $table->addColumn('created_at', Types::DATETIME, [
                'notnull' => true,
            ]);

            $table->addColumn('created_by', Types::STRING, [
                'notnull' => true,
                'length' => 64,
            ]);

            $table->addIndex(['year_from'], 'mv_fee_rules_year_idx');

            $schemaChanged = true;
        }

        // ── Fee records table ────────────────────────────────────────
        if (!$schema->hasTable('mv_fee_records')) {
            $table = $schema->createTable('mv_fee_records');

            $table->addColumn('id', Types::BIGINT, [
                'autoincrement' => true,
                'notnull' => true,
                'unsigned' => true,
            ]);
            $table->setPrimaryKey(['id']);

            $table->addColumn('member_id', Types::BIGINT, [
                'notnull' => true,
                'unsigned' => true,
            ]);

            $table->addColumn('year', Types::INTEGER, [
                'notnull' => true,
            ]);

            $table->addColumn('amount', Types::DECIMAL, [
                'notnull' => false,
                'precision' => 10,
                'scale' => 2,
            ]);

            $table->addColumn('rule_snapshot_json', Types::TEXT, [
                'notnull' => false,
            ]);

            $table->addColumn('manuell_angepasst', Types::BOOLEAN, [
                'notnull' => true,
                'default' => false,
            ]);

            $table->addColumn('paid', Types::BOOLEAN, [
                'notnull' => true,
                'default' => false,
            ]);

            $table->addColumn('payment_date', Types::DATE, [
                'notnull' => false,
            ]);

            $table->addColumn('notes', Types::TEXT, [
                'notnull' => false,
            ]);

            $table->addColumn('created_at', Types::DATETIME, [
                'notnull' => true,
            ]);

            $table->addColumn('updated_at', Types::DATETIME, [
                'notnull' => true,
            ]);

            $table->addUniqueIndex(['member_id', 'year'], 'mv_fee_records_member_year_idx');
            $table->addIndex(['year'], 'mv_fee_records_year_idx');
            $table->addIndex(['paid'], 'mv_fee_records_paid_idx');

            $schemaChanged = true;
        }

        return $schemaChanged ? $schema : null;
    }
}
