<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260324162229 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Create homework_submissions and submission_attachments tables';
    }

    public function up(Schema $schema): void
    {
        $this->addSql('CREATE TABLE homework_submissions (
            id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
            tenant_id UUID NOT NULL,
            homework_id UUID NOT NULL REFERENCES homework(id),
            student_id UUID NOT NULL REFERENCES users(id),
            response_html TEXT,
            status VARCHAR(20) NOT NULL DEFAULT \'draft\',
            submitted_at TIMESTAMPTZ,
            created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
            updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
            UNIQUE (homework_id, student_id)
        )');

        $this->addSql('CREATE TABLE submission_attachments (
            id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
            submission_id UUID NOT NULL REFERENCES homework_submissions(id) ON DELETE CASCADE,
            filename VARCHAR(255) NOT NULL,
            file_path VARCHAR(500) NOT NULL,
            file_size INT NOT NULL,
            mime_type VARCHAR(100) NOT NULL,
            uploaded_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
        )');

        $this->addSql('CREATE INDEX idx_submission_homework_tenant ON homework_submissions(homework_id, tenant_id)');
        $this->addSql('CREATE INDEX idx_submission_lookup ON homework_submissions(homework_id, student_id, tenant_id)');
    }

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