<?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 Lager-related tables (camps, participants, Stufen links, files).
 *
 * Part of Issue #54.
 */
class Version000009Date20260407000008 extends SimpleMigrationStep {

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

        // ── Lager (camps) table ──────────────────────────────────────
        if (!$schema->hasTable('mv_lager')) {
            $table = $schema->createTable('mv_lager');

            $table->addColumn('id', Types::BIGINT, [
                'autoincrement' => true,
                'notnull' => true,
                'unsigned' => true,
            ]);
            $table->addColumn('name', Types::STRING, [
                'notnull' => true,
                'length' => 255,
            ]);
            $table->addColumn('startdatum', Types::DATE, [
                'notnull' => true,
            ]);
            $table->addColumn('enddatum', Types::DATE, [
                'notnull' => true,
            ]);
            $table->addColumn('ort', Types::STRING, [
                'notnull' => false,
                'length' => 255,
            ]);
            $table->addColumn('beschreibung', Types::TEXT, [
                'notnull' => false,
            ]);
            $table->addColumn('files_folder_path', Types::STRING, [
                'notnull' => false,
                'length' => 1024,
            ]);
            $table->addColumn('created_at', Types::DATETIME, [
                'notnull' => true,
            ]);
            $table->addColumn('updated_at', Types::DATETIME, [
                'notnull' => true,
            ]);

            $table->setPrimaryKey(['id']);
            $table->addIndex(['startdatum'], 'mv_lager_start_idx');

            $schemaChanged = true;
        }

        // ── Lager-Stufen m:n table ───────────────────────────────────
        if (!$schema->hasTable('mv_lager_stufen')) {
            $table = $schema->createTable('mv_lager_stufen');

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

            $table->setPrimaryKey(['lager_id', 'stufe_id']);
            $table->addIndex(['stufe_id'], 'mv_lager_stufen_stufe_idx');

            $schemaChanged = true;
        }

        // ── Lager-Teilnehmer m:n table ───────────────────────────────
        if (!$schema->hasTable('mv_lager_teilnehmer')) {
            $table = $schema->createTable('mv_lager_teilnehmer');

            $table->addColumn('lager_id', Types::BIGINT, [
                'notnull' => true,
                'unsigned' => true,
            ]);
            $table->addColumn('member_id', Types::BIGINT, [
                'notnull' => true,
                'unsigned' => true,
            ]);
            $table->addColumn('rolle', Types::STRING, [
                'notnull' => true,
                'length' => 50,
                'default' => 'Teilnehmer',
            ]);

            $table->setPrimaryKey(['lager_id', 'member_id']);
            $table->addIndex(['member_id'], 'mv_lager_teiln_member_idx');

            $schemaChanged = true;
        }

        // ── Lager-Files table ────────────────────────────────────────
        if (!$schema->hasTable('mv_lager_files')) {
            $table = $schema->createTable('mv_lager_files');

            $table->addColumn('id', Types::BIGINT, [
                'autoincrement' => true,
                'notnull' => true,
                'unsigned' => true,
            ]);
            $table->addColumn('lager_id', Types::BIGINT, [
                'notnull' => true,
                'unsigned' => true,
            ]);
            $table->addColumn('file_path', Types::STRING, [
                'notnull' => true,
                'length' => 1024,
            ]);
            $table->addColumn('label', Types::STRING, [
                'notnull' => false,
                'length' => 255,
            ]);

            $table->setPrimaryKey(['id']);
            $table->addIndex(['lager_id'], 'mv_lager_files_lager_idx');

            $schemaChanged = true;
        }

        return $schemaChanged ? $schema : null;
    }
}
