<?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 the members table (oc_mv_members).
 *
 * Part of Issue #18 — Set up DB migrations for members table.
 * Foreign-key columns (stufe_id, family_id) are created here as nullable
 * integers; actual FK constraints will be added by later migrations
 * (Issues #30 and #27) once the referenced tables exist.
 */
class Version000001Date20260407000000 extends SimpleMigrationStep {

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

        if ($schema->hasTable('mv_members')) {
            return null;
        }

        $table = $schema->createTable('mv_members');

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

        // ── Core personal data ───────────────────────────────────────
        $table->addColumn('vorname', Types::STRING, [
            'notnull' => true,
            'length' => 255,
        ]);

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

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

        $table->addColumn('geschlecht', Types::STRING, [
            'notnull' => false,
            'length' => 20,
        ]);

        // ── Organisational fields ────────────────────────────────────
        $table->addColumn('rolle', Types::STRING, [
            'notnull' => true,
            'length' => 30,
            'default' => 'mitglied',
        ]);

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

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

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

        $table->addColumn('status', Types::STRING, [
            'notnull' => true,
            'length' => 20,
            'default' => 'aktiv',
        ]);

        // ── Sensitive / free-text fields ─────────────────────────────
        $table->addColumn('allergien_encrypted', Types::TEXT, [
            'notnull' => false,
        ]);

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

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

        // ── Health insurance (Krankenversicherung) ───────────────────
        $table->addColumn('kv_typ', Types::STRING, [
            'notnull' => false,
            'length' => 20,
        ]);

        $table->addColumn('kv_name', Types::STRING, [
            'notnull' => false,
            'length' => 255,
        ]);

        // ── Family relation ──────────────────────────────────────────
        $table->addColumn('family_id', Types::BIGINT, [
            'notnull' => false,
            'unsigned' => true,
        ]);

        // ── Fee snapshot ─────────────────────────────────────────────
        $table->addColumn('frozen_fee_rate', Types::DECIMAL, [
            'notnull' => false,
            'precision' => 10,
            'scale' => 2,
        ]);

        // ── Nextcloud integration URIs ───────────────────────────────
        $table->addColumn('calendar_event_uri', Types::STRING, [
            'notnull' => false,
            'length' => 512,
        ]);

        $table->addColumn('contact_vcard_uri', Types::STRING, [
            'notnull' => false,
            'length' => 512,
        ]);

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

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

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

        // ── Indexes ──────────────────────────────────────────────────
        $table->addIndex(['status'], 'mv_members_status_idx');
        $table->addIndex(['family_id'], 'mv_members_family_idx');
        $table->addIndex(['stufe_id'], 'mv_members_stufe_idx');
        $table->addIndex(['nachname'], 'mv_members_nachname_idx');
        $table->addIndex(['deleted_at'], 'mv_members_deleted_idx');
        $table->addIndex(['status', 'deleted_at'], 'mv_members_status_del_idx');

        return $schema;
    }
}
