import { Hono } from 'hono';
import { z } from 'zod';

import { queryAlbums, queryPhotos } from '@/features/iphoto';

const limitParam = z.coerce.number().int().positive().max(500).default(100);
const sinceParam = z.string().optional();
const deviceIdParam = z.string().optional();
const albumIdParam = z.string().min(1);

export const photosMyRouter = new Hono()
  .get('/', async (c) => {
    const limit = limitParam.parse(c.req.query('limit') ?? '100');
    const since = sinceParam.parse(c.req.query('since'));
    const deviceId = deviceIdParam.parse(c.req.query('deviceId'));
    return c.json(await queryPhotos({ limit, since, deviceId }));
  })
  .get('/albums', async (c) => {
    const deviceId = z.string().min(1).parse(c.req.query('deviceId'));
    return c.json(await queryAlbums(deviceId));
  })
  .get('/albums/:albumId', async (c) => {
    const albumId = albumIdParam.parse(c.req.param('albumId'));
    const limit = limitParam.parse(c.req.query('limit') ?? '100');
    return c.json(await queryPhotos({ albumId, limit }));
  });
