import test from 'node:test';
import assert from 'node:assert/strict';
import { dormitoryImportStudentSchema, hostelOnboardStudentSchema, onboardStudentSchema } from './students.schemas';

test('a paid UPI deposit allows an optional UPI ID or UTR reference', () => {
  const missing = onboardStudentSchema.safeParse({ name: 'QA Customer', depositPaid: 500, depositMethod: 'UPI' });
  assert.equal(missing.success, true);

  const valid = onboardStudentSchema.safeParse({ name: 'QA Customer', depositPaid: 500, depositMethod: 'UPI', depositReference: 'UTR123' });
  assert.equal(valid.success, true);
});

test('manual hostel onboarding requires the complete resident record', () => {
  const result = hostelOnboardStudentSchema.safeParse({ name: 'QA Resident' });
  assert.equal(result.success, false);
  if (result.success) return;
  const paths = new Set(result.error.issues.map((issue) => issue.path.join('.')));
  for (const field of [
    'phone',
    'guardianName',
    'guardianPhone',
    'address',
    'state',
    'city',
    'postalCode',
    'depositAmount',
    'depositPaid',
    'depositPaidAt',
    'depositMethod',
    'kycIdType',
    'kycIdNumber',
    'kycStatus',
    'idProofUrl',
    'photoUrl',
    'movedInAt',
  ]) {
    assert.equal(paths.has(field), true, `${field} should be required`);
  }
});

test('manual hostel onboarding accepts a complete resident record', () => {
  const result = hostelOnboardStudentSchema.safeParse({
    name: 'QA Resident',
    phone: '9876543210',
    guardianName: 'QA Guardian',
    guardianPhone: '9876543211',
    address: '1 Test Street',
    state: 'Tamil Nadu',
    city: 'Chennai',
    postalCode: '600001',
    depositAmount: 5000,
    depositPaid: 0,
    depositPaidAt: '2026-10-29',
    depositMethod: 'CASH',
    kycIdType: 'AADHAAR',
    kycIdNumber: '123456789012',
    kycStatus: 'PENDING',
    idProofUrl: 'tenants/test/students/id-proof/document.pdf',
    photoUrl: 'tenants/test/students/photo/resident.jpg',
    movedInAt: '2026-10-29',
  });
  assert.equal(result.success, true);
});

test('base dormitory onboarding remains compatible with existing clients', () => {
  const result = onboardStudentSchema.safeParse({
    name: 'Imported Resident',
    email: 'imported@example.com',
    password: 'Password123',
  });
  assert.equal(result.success, true);
  if (result.success) assert.equal(result.data.bedId, undefined);
});

test('dormitory imports require complete details and allow missing guardians', () => {
  const missing = dormitoryImportStudentSchema.safeParse({ name: 'QA Customer' });
  assert.equal(missing.success, false);
  if (missing.success) return;
  const paths = new Set(missing.error.issues.map((issue) => issue.path.join('.')));
  for (const field of ['phone', 'address', 'state', 'city', 'postalCode', 'depositAmount', 'depositPaid', 'depositPaidAt', 'depositMethod', 'kycIdType', 'kycIdNumber', 'kycStatus', 'idProofUrl', 'photoUrl', 'movedInAt']) {
    assert.equal(paths.has(field), true, `${field} should be required`);
  }
  assert.equal(paths.has('guardianName'), false);
  assert.equal(paths.has('guardianPhone'), false);

  const valid = dormitoryImportStudentSchema.safeParse({
    name: 'QA Customer', phone: '9876543210', address: '12 MG Road', state: 'Maharashtra', city: 'Pune', postalCode: '411001',
    depositAmount: 1000, depositPaid: 0, depositPaidAt: '2026-10-30', depositMethod: 'CASH',
    kycIdType: 'AADHAAR', kycIdNumber: '123412341234', kycStatus: 'PENDING',
    idProofUrl: 'tenants/test/students/id_proof/document.pdf', photoUrl: 'tenants/test/students/photo/customer.jpg', movedInAt: '2026-10-30',
  });
  assert.equal(valid.success, true);
  if (valid.success) assert.equal(valid.data.bedId, undefined);
});
