<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20260312002349 extends AbstractMigration
{
    public function getDescription(): string
    {
        return 'Create homework and homework_attachments tables for Story 5.1';
    }

    public function up(Schema $schema): void
    {
        $this->addSql(<<<'SQL'
            CREATE TABLE homework (
                id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                tenant_id UUID NOT NULL,
                class_id UUID NOT NULL REFERENCES school_classes(id),
                subject_id UUID NOT NULL REFERENCES subjects(id),
                teacher_id UUID NOT NULL REFERENCES users(id),
                title VARCHAR(255) NOT NULL,
                description TEXT,
                due_date DATE NOT NULL,
                status VARCHAR(20) DEFAULT 'published' NOT NULL,
                created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
                updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
            )
            SQL);

        $this->addSql('CREATE INDEX idx_homework_tenant ON homework(tenant_id)');
        $this->addSql('CREATE INDEX idx_homework_class ON homework(class_id)');
        $this->addSql('CREATE INDEX idx_homework_due_date ON homework(due_date)');
        $this->addSql('CREATE INDEX idx_homework_teacher ON homework(teacher_id)');

        $this->addSql(<<<'SQL'
            CREATE TABLE homework_attachments (
                id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                homework_id UUID NOT NULL REFERENCES homework(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()
            )
            SQL);

        $this->addSql('CREATE INDEX idx_homework_attachments_homework ON homework_attachments(homework_id)');
    }

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