import { describe, it, expect } from 'vitest';
import {
  createFolderSchema,
  renameFolderSchema,
  moveFolderSchema,
  moveDocumentToFolderSchema,
} from '@/lib/validators/document-folders';
import { listDocumentsSchema } from '@/lib/validators/documents';

describe('document-folder validators', () => {
  it('accepts a valid create payload', () => {
    expect(createFolderSchema.safeParse({ name: 'Deals', parentId: null }).success).toBe(true);
    expect(createFolderSchema.safeParse({ name: 'Q1', parentId: 'abc-123' }).success).toBe(true);
  });

  it('rejects empty + over-long names', () => {
    expect(createFolderSchema.safeParse({ name: '', parentId: null }).success).toBe(false);
    expect(createFolderSchema.safeParse({ name: 'x'.repeat(201), parentId: null }).success).toBe(
      false,
    );
  });

  it('rejects whitespace-only names', () => {
    expect(createFolderSchema.safeParse({ name: '   ', parentId: null }).success).toBe(false);
  });

  it('rename schema requires only name', () => {
    expect(renameFolderSchema.safeParse({ name: 'New' }).success).toBe(true);
    expect(renameFolderSchema.safeParse({ name: '' }).success).toBe(false);
  });

  it('move schema accepts null parentId', () => {
    expect(moveFolderSchema.safeParse({ parentId: null }).success).toBe(true);
    expect(moveFolderSchema.safeParse({ parentId: 'abc' }).success).toBe(true);
  });

  it('moveDocumentToFolderSchema accepts null folderId', () => {
    expect(moveDocumentToFolderSchema.safeParse({ folderId: null }).success).toBe(true);
    expect(moveDocumentToFolderSchema.safeParse({ folderId: 'abc' }).success).toBe(true);
  });

  // ─── folderId='' → null transform (regression) ─────────────────────────────
  //
  // The frontend's URL-query builder emits `?folderId=` (empty string) when
  // the user picks "All documents" - without the transform, Zod would parse
  // this as the literal string "" and the SQL layer would try to JOIN on an
  // empty folder id, returning zero rows instead of the expected unscoped
  // result. The transform lives on `listDocumentsSchema` (and
  // `listFilesSchema`); we exercise the documents one here.
  describe("listDocumentsSchema folderId='' transform", () => {
    const BASE = { page: 1, limit: 20, sort: 'createdAt', order: 'desc', includeArchived: 'false' };

    it("coerces folderId='' to null so the empty-string query param means 'unscoped'", () => {
      const result = listDocumentsSchema.safeParse({ ...BASE, folderId: '' });
      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.folderId).toBeNull();
      }
    });

    it('leaves a non-empty folderId untouched', () => {
      const id = crypto.randomUUID();
      const result = listDocumentsSchema.safeParse({ ...BASE, folderId: id });
      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.folderId).toBe(id);
      }
    });

    it('accepts an explicit folderId=null (no transform needed)', () => {
      const result = listDocumentsSchema.safeParse({ ...BASE, folderId: null });
      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.folderId).toBeNull();
      }
    });

    it('treats an absent folderId as undefined (not the same as null)', () => {
      const result = listDocumentsSchema.safeParse(BASE);
      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.folderId).toBeUndefined();
      }
    });
  });
});
