import { z } from 'zod';

// Base types - IDs are integers matching Drizzle schema
export const ProjectSchema = z.object({
  id: z.number().int().positive(),
  name: z.string().min(1).max(255),
  description: z.string().nullable(),
  userId: z.number().int().positive(),
  isPublic: z.boolean(),
  createdAt: z.date(),
  updatedAt: z.date(),
});

export const CharacterSchema = z.object({
  id: z.number().int().positive(),
  name: z.string().min(1).max(100),
  slug: z.string(),
  description: z.string().nullable(),
  bio: z.string().nullable(),
  role: z.enum(['protagonist', 'antagonist', 'supporting', 'background', 'ensemble']),
  arc: z.string().nullable(),
  arcType: z.enum(['positive', 'negative', 'flat', 'complex']).nullable(),
  age: z.number().int().nullable(),
  gender: z.string().nullable(),
  voice: z.string().nullable(),
  traits: z.string().nullable(),
  motivation: z.string().nullable(),
  conflict: z.string().nullable(),
  secret: z.string().nullable(),
  imageUrl: z.string().nullable(),
  projectId: z.number().int().positive(),
  createdAt: z.date(),
  updatedAt: z.date(),
});

export const CharacterRelationshipSchema = z.object({
  id: z.number().int().positive(),
  characterIdA: z.number().int().positive(),
  characterIdB: z.number().int().positive(),
  relationshipType: z.enum(['family', 'romantic', 'friendship', 'rivalry', 'mentor', 'alliance', 'conflict', 'professional', 'other']),
  description: z.string().nullable(),
  strength: z.number().int().min(0).max(100),
  isAntagonistic: z.boolean(),
  createdAt: z.date(),
  updatedAt: z.date(),
});

export const CharacterStatsSchema = z.object({
  characterId: z.number().int().positive(),
  totalScreenTime: z.number().int(),
  totalDialogueLines: z.number().int(),
  sceneCount: z.number().int(),
  relationshipCount: z.number().int(),
});

export const SceneSchema = z.object({
  id: z.number().int().positive(),
  title: z.string().min(1),
  content: z.string(),
  projectId: z.number().int().positive(),
  order: z.number().int().nonnegative(),
  createdAt: z.date(),
  updatedAt: z.date(),
});

// Input schemas
export const CreateProjectInputSchema = z.object({
  name: z.string().min(1).max(255),
  description: z.string().optional(),
});

export const UpdateProjectInputSchema = z.object({
  id: z.number().int().positive(),
  name: z.string().min(1).max(255).optional(),
  description: z.string().optional(),
});

export const CreateCharacterInputSchema = z.object({
  name: z.string().min(1).max(100),
  description: z.string().optional(),
  bio: z.string().optional(),
  role: z.enum(['protagonist', 'antagonist', 'supporting', 'background', 'ensemble']).optional(),
  arc: z.string().optional(),
  arcType: z.enum(['positive', 'negative', 'flat', 'complex']).optional(),
  age: z.number().int().optional(),
  gender: z.string().optional(),
  voice: z.string().optional(),
  traits: z.string().optional(),
  motivation: z.string().optional(),
  conflict: z.string().optional(),
  secret: z.string().optional(),
  imageUrl: z.string().url().optional(),
  projectId: z.number().int().positive(),
});

export const UpdateCharacterInputSchema = z.object({
  id: z.number().int().positive(),
  name: z.string().min(1).max(100).optional(),
  description: z.string().optional(),
  bio: z.string().optional(),
  role: z.enum(['protagonist', 'antagonist', 'supporting', 'background', 'ensemble']).optional(),
  arc: z.string().optional(),
  arcType: z.enum(['positive', 'negative', 'flat', 'complex']).optional(),
  age: z.number().int().optional(),
  gender: z.string().optional(),
  voice: z.string().optional(),
  traits: z.string().optional(),
  motivation: z.string().optional(),
  conflict: z.string().optional(),
  secret: z.string().optional(),
  imageUrl: z.string().url().optional(),
  projectId: z.number().int().positive().optional(),
});

export const CreateRelationshipInputSchema = z.object({
  characterIdA: z.number().int().positive(),
  characterIdB: z.number().int().positive(),
  relationshipType: z.enum(['family', 'romantic', 'friendship', 'rivalry', 'mentor', 'alliance', 'conflict', 'professional', 'other']),
  description: z.string().optional(),
  strength: z.number().int().min(0).max(100).optional(),
  isAntagonistic: z.boolean().optional(),
});

export const UpdateRelationshipInputSchema = z.object({
  id: z.number().int().positive(),
  relationshipType: z.enum(['family', 'romantic', 'friendship', 'rivalry', 'mentor', 'alliance', 'conflict', 'professional', 'other']).optional(),
  description: z.string().optional(),
  strength: z.number().int().min(0).max(100).optional(),
  isAntagonistic: z.boolean().optional(),
});

export const SearchCharactersInputSchema = z.object({
  projectId: z.number().int().positive(),
  query: z.string().optional(),
  role: z.enum(['protagonist', 'antagonist', 'supporting', 'background', 'ensemble']).optional(),
  arcType: z.enum(['positive', 'negative', 'flat', 'complex']).optional(),
});

export const CreateSceneInputSchema = z.object({
  title: z.string().min(1),
  content: z.string().optional(),
  projectId: z.number().int().positive(),
  order: z.number().int().nonnegative(),
});

export const UpdateSceneInputSchema = z.object({
  id: z.number().int().positive(),
  title: z.string().min(1).optional(),
  content: z.string().optional(),
  order: z.number().int().nonnegative().optional(),
});

// Response schemas
export const ProjectListSchema = z.array(ProjectSchema);
export const CharacterListSchema = z.array(CharacterSchema);
export const CharacterRelationshipListSchema = z.array(CharacterRelationshipSchema);
export const CharacterStatsListSchema = z.array(CharacterStatsSchema);
export const SceneListSchema = z.array(SceneSchema);

// Auth context
export interface TRPCContext {
  userId?: number;
  clerkUserId?: string;
  projectId?: number;
  db?: import('../../src/db/config/migrations').DrizzleDB;
}
