/**
 * In-App Feedback module — types and schemas.
 * Users submit feedback (bug, feature, praise); admins triage.
 */

import { z } from 'zod';

export type FeedbackType = 'bug' | 'feature' | 'praise' | 'other';
export type FeedbackStatus = 'new' | 'reviewed' | 'planned' | 'resolved' | 'wont_fix';

export interface FeedbackDoc {
  id: string;
  productId: string;
  userId: string;
  type: FeedbackType;
  title: string;
  body: string;
  rating: number | null; // 1–5 optional satisfaction rating
  screen: string | null; // which screen/page the feedback came from
  appVersion: string | null;
  platform: string | null; // web, ios, android
  status: FeedbackStatus;
  adminNotes: string | null;

  // Screenshot attachment (Phase 1.1)
  screenshotBlobPath?: string; // "feedback/{productId}/{feedbackId}/{screenshotId}.png"
  screenshotUrl?: string; // Time-limited SAS URL for viewing
  screenshotUrlExpiresAt?: string; // When SAS URL expires
  screenshotContentType?: 'image/png' | 'image/jpeg' | 'image/webp';
  screenshotSizeBytes?: number;

  // Device context for debugging (Phase 1.1)
  deviceContext?: {
    osVersion: string;
    appVersion: string;
    deviceModel: string;
    screenResolution: string;
    locale: string;
  };

  createdAt: string;
  updatedAt: string;
}

// ── Schemas ────────────────────────────────────────────────────

export const CreateFeedbackSchema = z.object({
  type: z.enum(['bug', 'feature', 'praise', 'other']),
  title: z.string().min(1).max(200),
  body: z.string().max(5000).default(''),
  rating: z.number().int().min(1).max(5).nullable().default(null),
  screen: z.string().max(100).nullable().default(null),
  appVersion: z.string().max(50).nullable().default(null),
  platform: z.string().max(20).nullable().default(null),
  // Screenshot fields (Phase 1.1)
  screenshotBlobPath: z.string().optional(),
  screenshotContentType: z.enum(['image/png', 'image/jpeg', 'image/webp']).optional(),
  screenshotSizeBytes: z
    .number()
    .int()
    .min(1)
    .max(10 * 1024 * 1024)
    .optional(), // 10MB max
  // Device context (Phase 1.1)
  deviceContext: z
    .object({
      osVersion: z.string().max(50),
      appVersion: z.string().max(50),
      deviceModel: z.string().max(100),
      screenResolution: z.string().max(20),
      locale: z.string().max(10),
    })
    .optional(),
});

export const UpdateFeedbackSchema = z.object({
  status: z.enum(['new', 'reviewed', 'planned', 'resolved', 'wont_fix']).optional(),
  adminNotes: z.string().max(2000).nullable().optional(),
});

export const QueryFeedbackSchema = z.object({
  type: z.enum(['bug', 'feature', 'praise', 'other']).optional(),
  status: z.enum(['new', 'reviewed', 'planned', 'resolved', 'wont_fix']).optional(),
  limit: z.coerce.number().int().min(1).max(100).default(50),
  offset: z.coerce.number().int().min(0).default(0),
});

export type CreateFeedbackInput = z.infer<typeof CreateFeedbackSchema>;
export type UpdateFeedbackInput = z.infer<typeof UpdateFeedbackSchema>;
export type QueryFeedbackInput = z.infer<typeof QueryFeedbackSchema>;
