<?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 contact cards staging table for ContactsSync.
 *
 * Part of Issue #51.
 */
class Version000012Date20260407000011 extends SimpleMigrationStep {

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

        if (!$schema->hasTable('mv_contact_cards')) {
            $table = $schema->createTable('mv_contact_cards');

            $table->addColumn('id', Types::BIGINT, [
                'autoincrement' => true,
                'notnull' => true,
                'unsigned' => true,
            ]);
            $table->addColumn('vcard_uri', Types::STRING, [
                'notnull' => true,
                'length' => 255,
            ]);
            $table->addColumn('vcard_data', Types::TEXT, [
                'notnull' => true,
            ]);
            $table->addColumn('addressbook_uri', Types::STRING, [
                'notnull' => true,
                'length' => 255,
            ]);
            $table->addColumn('created_at', Types::DATETIME, [
                'notnull' => true,
            ]);
            $table->addColumn('updated_at', Types::DATETIME, [
                'notnull' => true,
            ]);

            $table->setPrimaryKey(['id']);
            $table->addUniqueIndex(['vcard_uri'], 'mv_contact_vcard_uri_uniq');
            $table->addIndex(['addressbook_uri'], 'mv_contact_ab_idx');

            $schemaChanged = true;
        }

        return $schemaChanged ? $schema : null;
    }
}
