<?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 addresses, phones, and emails tables.
 *
 * Part of Issue #19 — Set up DB migrations for addresses, phones, emails.
 * These are sub-entity tables linked to oc_mv_members via member_id FK
 * with CASCADE delete.
 */
class Version000002Date20260407000001 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();
        $schemaChanged = false;

        // ── Addresses table ──────────────────────────────────────────
        if (!$schema->hasTable('mv_addresses')) {
            $table = $schema->createTable('mv_addresses');

            $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('label', Types::STRING, [
                'notnull' => false,
                'length' => 100,
            ]);

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

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

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

            $table->addColumn('land', Types::STRING, [
                'notnull' => true,
                'length' => 100,
                'default' => 'Deutschland',
            ]);

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

            $table->addIndex(['member_id'], 'mv_addresses_member_idx');

            $schemaChanged = true;
        }

        // ── Phones table ─────────────────────────────────────────────
        if (!$schema->hasTable('mv_phones')) {
            $table = $schema->createTable('mv_phones');

            $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('label', Types::STRING, [
                'notnull' => false,
                'length' => 100,
            ]);

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

            $table->addIndex(['member_id'], 'mv_phones_member_idx');

            $schemaChanged = true;
        }

        // ── Emails table ─────────────────────────────────────────────
        if (!$schema->hasTable('mv_emails')) {
            $table = $schema->createTable('mv_emails');

            $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('label', Types::STRING, [
                'notnull' => false,
                'length' => 100,
            ]);

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

            $table->addIndex(['member_id'], 'mv_emails_member_idx');

            $schemaChanged = true;
        }

        return $schemaChanged ? $schema : null;
    }
}
