import { Router } from 'express';
import { Role } from '@prisma/client';
import { asyncHandler } from '../../middleware/asyncHandler';
import { authenticate } from '../../middleware/authenticate';
import { authorize } from '../../middleware/authorize';
import { requirePermission } from '../../middleware/requirePermission';
import { propertyContext } from '../../middleware/propertyContext';
import * as ctrl from './students.controller';
import { importStudentsHandler } from './students-import';

export const studentRouter = Router();

// Student self-service — must be declared before the management guard below.
studentRouter.get('/me', authenticate, authorize(Role.STUDENT), asyncHandler(ctrl.me));
studentRouter.patch('/me', authenticate, authorize(Role.STUDENT), asyncHandler(ctrl.updateMe));
studentRouter.get('/me/doc-url', authenticate, authorize(Role.STUDENT), asyncHandler(ctrl.myDocUrl));
studentRouter.put('/me/mandate', authenticate, authorize(Role.STUDENT), asyncHandler(ctrl.setMyMandate));
// A student may replace their own photo, and nothing else about their record.
studentRouter.patch(
  '/me/photo',
  authenticate,
  authorize(Role.STUDENT),
  asyncHandler(ctrl.updateMyPhoto),
);

// Management endpoints — anyone with MANAGE_STUDENTS.
const manage = [authenticate, propertyContext, requirePermission('MANAGE_STUDENTS')] as const;
studentRouter.post('/', ...manage, asyncHandler(ctrl.onboardStudent));
// Import from the owner's Excel or CSV sheet (L5): it can make rooms and beds, so it needs both permissions.
studentRouter.post('/import', ...manage, requirePermission('MANAGE_ROOMS'), asyncHandler(importStudentsHandler));
// Before '/:id'-style routes so the literal path is not swallowed by a param.
studentRouter.get('/stats', ...manage, asyncHandler(ctrl.studentStats));
studentRouter.get('/', ...manage, asyncHandler(ctrl.listStudents));
// Param routes last, after the literal paths above.
studentRouter.get('/:id', ...manage, asyncHandler(ctrl.getStudent));
studentRouter.patch('/:id', ...manage, asyncHandler(ctrl.updateStudent));
studentRouter.delete('/:id', ...manage, asyncHandler(ctrl.deleteStudent));
studentRouter.post(
  '/:id/reset-password',
  ...manage,
  asyncHandler(ctrl.resetStudentPassword),
);
// A walk-in registered without a login can be given one later.
studentRouter.post('/:id/login', ...manage, asyncHandler(ctrl.createStudentLogin));
studentRouter.patch('/:id/bed', ...manage, asyncHandler(ctrl.assignBed));
studentRouter.patch('/:id/biometric', ...manage, asyncHandler(ctrl.setBiometricId));
// Sprint 1 (TEPL): a bed held until the employee's first punch.
studentRouter.patch('/:id/arrival', ...manage, asyncHandler(ctrl.holdArrival));

// One-off charges are money, so they need MANAGE_RENT rather than MANAGE_STUDENTS.
const money = [authenticate, propertyContext, requirePermission('MANAGE_RENT')] as const;
studentRouter.get('/:id/charges', ...money, asyncHandler(ctrl.listCharges));
// Smart Collect: a bank account + UPI id that is this student's alone.
studentRouter.get('/:id/virtual-account', ...money, asyncHandler(ctrl.getVirtualAccount));
studentRouter.post('/:id/virtual-account', ...money, asyncHandler(ctrl.createVirtualAccount));
studentRouter.delete('/:id/virtual-account', ...money, asyncHandler(ctrl.closeVirtualAccount));
// UPI Autopay groundwork: store the mandate a student set up; no debits.
studentRouter.put('/:id/mandate', ...money, asyncHandler(ctrl.setMandate));
studentRouter.post('/:id/charges', ...money, asyncHandler(ctrl.addCharge));
