/**
 * PR8 — MentorFile schema invariant check
 *
 * The actual data migration (backfill of MentorFile.projectId from the
 * originating MentorAssignment.projectId) was verified against the May 7
 * production database dump in Task 2 of PR8. This file is a complementary
 * schema-invariant check that runs against the current dev DB:
 *
 *   1. MentorFile.projectId is now a required column (Prisma validation fails
 *      when omitted).
 *   2. Files are scoped to the project, not to a single MentorAssignment —
 *      deleting the originating assignment leaves the file in place with
 *      mentorAssignmentId set to NULL (FK SetNull) and projectId unchanged.
 *      This is what enables team-wide file visibility across co-mentors.
 */

import { afterAll, describe, expect, it } from 'vitest'
import { prisma } from '../setup'
import {
  createTestUser,
  createTestProgram,
  createTestProject,
  cleanupTestData,
  uid,
} from '../helpers'

describe('MentorFile scope invariants (PR8 schema)', () => {
  const programIds: string[] = []
  const userIds: string[] = []
  const mentorFileIds: string[] = []

  afterAll(async () => {
    if (mentorFileIds.length > 0) {
      await prisma.mentorFile.deleteMany({ where: { id: { in: mentorFileIds } } })
    }
    for (const programId of programIds) {
      await prisma.mentorAssignment.deleteMany({ where: { project: { programId } } })
      await prisma.mentorFile.deleteMany({ where: { project: { programId } } })
      await cleanupTestData(programId, [])
    }
    if (userIds.length > 0) {
      await prisma.user.deleteMany({ where: { id: { in: userIds } } })
    }
  })

  it('MentorFile.projectId matches MentorAssignment.projectId when created via the workspace path', async () => {
    const program = await createTestProgram({ name: `mfscope-match-${uid()}` })
    programIds.push(program.id)
    const project = await createTestProject(program.id, { title: 'Scope Match' })
    const mentor = await createTestUser('MENTOR')
    userIds.push(mentor.id)

    const assignment = await prisma.mentorAssignment.create({
      data: {
        projectId: project.id,
        mentorId: mentor.id,
        method: 'MANUAL',
        workspaceEnabled: true,
      },
    })

    const file = await prisma.mentorFile.create({
      data: {
        projectId: project.id,
        mentorAssignmentId: assignment.id,
        uploadedByUserId: mentor.id,
        fileName: 'invariant.pdf',
        mimeType: 'application/pdf',
        size: 1024,
        bucket: 'mopc-files',
        objectKey: `Scope_Match/mentorship/${Date.now()}-invariant.pdf`,
      },
    })
    mentorFileIds.push(file.id)

    expect(file.projectId).toBe(assignment.projectId)
  })

  it('creating a MentorFile without a projectId is rejected by Prisma', async () => {
    const program = await createTestProgram({ name: `mfscope-noproj-${uid()}` })
    programIds.push(program.id)
    const mentor = await createTestUser('MENTOR')
    userIds.push(mentor.id)

    // `projectId` is required in the schema — Prisma should reject this.
    // Cast away the type for the deliberate omission.
    await expect(
      prisma.mentorFile.create({
        data: {
          uploadedByUserId: mentor.id,
          fileName: 'no-project.pdf',
          mimeType: 'application/pdf',
          size: 10,
          bucket: 'mopc-files',
          objectKey: 'orphan/no-project.pdf',
        } as unknown as Parameters<typeof prisma.mentorFile.create>[0]['data'],
      }),
    ).rejects.toThrow()
  })

  it('dropping the originating MentorAssignment leaves the MentorFile in place (SetNull)', async () => {
    const program = await createTestProgram({ name: `mfscope-setnull-${uid()}` })
    programIds.push(program.id)
    const project = await createTestProject(program.id, { title: 'SetNull Project' })
    const mentor = await createTestUser('MENTOR')
    userIds.push(mentor.id)

    const assignment = await prisma.mentorAssignment.create({
      data: {
        projectId: project.id,
        mentorId: mentor.id,
        method: 'MANUAL',
        workspaceEnabled: true,
      },
    })

    const file = await prisma.mentorFile.create({
      data: {
        projectId: project.id,
        mentorAssignmentId: assignment.id,
        uploadedByUserId: mentor.id,
        fileName: 'survives-drop.pdf',
        mimeType: 'application/pdf',
        size: 2048,
        bucket: 'mopc-files',
        objectKey: `SetNull_Project/mentorship/${Date.now()}-survives.pdf`,
      },
    })
    mentorFileIds.push(file.id)

    await prisma.mentorAssignment.delete({ where: { id: assignment.id } })

    const after = await prisma.mentorFile.findUnique({ where: { id: file.id } })
    expect(after).not.toBeNull()
    expect(after?.mentorAssignmentId).toBeNull()
    expect(after?.projectId).toBe(project.id)
  })
})
