import { Router } from 'express';
import { DeviceController } from '../controllers/device.controller';
import { validateRequest } from '../middleware/validation.middleware';
import {
  createDeviceSchema,
  deviceIdParamSchema,
  updateDeviceStatusSchema,
  updateDevicePushTokenSchema,
} from '../../application/dto';
import { paginationQuerySchema } from '../../application/dto/pagination.dto';

/**
 * Device routes mounted under `/api/v1`.
 *
 * Routes:
 *  - POST   /devices            Create or retrieve a device by fingerprint.
 *                               Body: { location, type, info?, ipAddress, userAgent?, acceptLanguage?, fingerprint, status? }
 *                               status: 'active' | 'background' | 'closed' (default: 'closed')
 *  - GET    /devices            List devices with pagination.
 *                               Query: { limit?: number, offset?: number }
 *  - GET    /devices/:id        Get a device by its UUID.
 *                               Param: { id: string (UUID) }
 *  - PATCH  /devices/:id/status Update a device status.
 *                               Param: { id: string (UUID) }
 *                               Body: { status: 'active' | 'background' | 'closed' }
 *  - PATCH  /devices/:id/push-token Update a device's OneSignal push token.
 *                               Param: { id: string (UUID) }
 *                               Body: { pushToken: string | null }
 */
export const createDeviceRoutes = (controller: DeviceController): Router => {
  const router = Router();

  router.post(
    '/devices',
    validateRequest({ body: createDeviceSchema }),
    controller.create,
  );

  router.get(
    '/devices',
    validateRequest({ query: paginationQuerySchema }),
    controller.getAll,
  );

  router.get(
    '/devices/:id',
    validateRequest({ params: deviceIdParamSchema }),
    controller.getById,
  );

  router.patch(
    '/devices/:id/status',
    validateRequest({
      params: deviceIdParamSchema,
      body: updateDeviceStatusSchema,
    }),
    controller.updateStatus,
  );

  router.patch(
    '/devices/:id/push-token',
    validateRequest({
      params: deviceIdParamSchema,
      body: updateDevicePushTokenSchema,
    }),
    controller.updatePushToken,
  );

  return router;
};
