<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
 * Creates the student_guardians table for parent-child linking.
 *
 * Each student can have at most 2 guardians (parents/tutors).
 * The constraint is enforced at application level (see StudentGuardian::MAX_GUARDIANS_PER_STUDENT).
 * The UNIQUE constraint on (student_id, guardian_id) prevents duplicate links.
 */
final class Version20260210100000 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Create student_guardians table for parent-child linking (Story 2.7)';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            CREATE TABLE IF NOT EXISTS student_guardians (
                id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                student_id UUID NOT NULL,
                guardian_id UUID NOT NULL,
                relationship_type VARCHAR(50) NOT NULL,
                created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                created_by UUID,
                tenant_id UUID NOT NULL,
                UNIQUE(student_id, guardian_id)
            )
        SQL);

        $this->addSql('CREATE INDEX IF NOT EXISTS idx_student_guardians_tenant ON student_guardians(tenant_id)');
        $this->addSql('CREATE INDEX IF NOT EXISTS idx_student_guardians_guardian ON student_guardians(guardian_id)');
        $this->addSql('CREATE INDEX IF NOT EXISTS idx_student_guardians_student ON student_guardians(student_id)');
    }

    public function down(Schema $schema): void
    {
        $this->addSql('DROP TABLE IF EXISTS student_guardians');
    }
}
