/**
 * Zod schemas for review API routes.
 */

import { z } from 'zod';

const uuidSchema = z.uuid('Must be a valid UUID');

// ---------------------------------------------------------------------------
// POST /api/reviews (submit review)
// ---------------------------------------------------------------------------

export const submitReviewBodySchema = z.discriminatedUnion('type', [
  z.object({
    type: z.literal('standard'),
    purchaseId: uuidSchema,
    rating: z.number().int().min(1).max(5),
    body: z.string().trim().min(1).max(4000),
    csrfToken: z.string().min(1).optional(),
  }),
  z.object({
    type: z.literal('technical'),
    purchaseId: uuidSchema,
    body: z.string().trim().min(1).max(4000),
    csrfToken: z.string().min(1).optional(),
  }),
]);

export type SubmitReviewBody = z.infer<typeof submitReviewBodySchema>;

export const submitReviewResponseSchema = z.object({
  ok: z.literal(true),
  reviewId: uuidSchema,
});

export type SubmitReviewResponse = z.infer<typeof submitReviewResponseSchema>;

// ---------------------------------------------------------------------------
// GET /api/reviews?vendorId=...&type=...
// ---------------------------------------------------------------------------

export const listReviewsQuerySchema = z
  .object({
    vendorId: uuidSchema.optional(),
    dealId: uuidSchema.optional(),
    type: z.enum(['standard', 'technical']).optional().default('standard'),
  })
  .refine((d) => d.vendorId !== undefined || d.dealId !== undefined, {
    message: 'Must provide vendorId or dealId',
  });

export type ListReviewsQuery = z.infer<typeof listReviewsQuerySchema>;

export const reviewSummarySchema = z.object({
  id: uuidSchema,
  purchaseId: uuidSchema,
  userId: uuidSchema,
  vendorId: uuidSchema,
  reviewType: z.enum(['STANDARD', 'TECHNICAL']),
  rating: z.number().int().min(1).max(5).nullable(),
  body: z.string(),
  vendorReply: z.string().nullable(),
  vendorReplyAt: z.iso.datetime().nullable(),
  isVisible: z.boolean(),
  createdAt: z.iso.datetime(),
});

export type ReviewSummary = z.infer<typeof reviewSummarySchema>;

export const listReviewsResponseSchema = z.object({
  ok: z.literal(true),
  reviews: z.array(reviewSummarySchema),
});

export type ListReviewsResponse = z.infer<typeof listReviewsResponseSchema>;

// ---------------------------------------------------------------------------
// POST /api/reviews/[id]/reply
// ---------------------------------------------------------------------------

export const replyToReviewBodySchema = z.object({
  reply: z.string().trim().min(1).max(2000),
  csrfToken: z.string().min(1).optional(),
});

export type ReplyToReviewBody = z.infer<typeof replyToReviewBodySchema>;

// ---------------------------------------------------------------------------
// POST /api/reviews/[id]/remove
// ---------------------------------------------------------------------------

export const requestReviewRemovalBodySchema = z.object({
  reason: z.string().trim().min(10).max(1000),
  csrfToken: z.string().min(1).optional(),
});

export type RequestReviewRemovalBody = z.infer<typeof requestReviewRemovalBodySchema>;
