export type UUID = string;

export type ProjectStatus = 'in_progress' | 'done' | 'archived';

export interface Project {
  id: UUID;
  title: string;
  status: ProjectStatus;
  tags: string[];
  coverImageUri?: string;
  createdAt: string;
  updatedAt: string;
}

export type StepType = 'forming' | 'trimming' | 'drying' | 'bisque_firing' | 'glazing' | 'glaze_firing' | 'misc';

export type TemperatureUnit = 'F' | 'C';

export interface Temperature {
  value: number;
  unit: TemperatureUnit;
}

// Weight types
export type WeightUnit = 'lb' | 'oz' | 'kg' | 'g';

export interface Weight {
  value: number;
  unit: WeightUnit;
}

// Production method for forming
export type ProductionMethod = 'wheel' | 'handbuilding' | 'casting' | 'other';

// Dimensions
export type DimensionUnit = 'in' | 'cm';

export interface Dimensions {
  height?: number;
  width?: number;
  diameter?: number;
  unit: DimensionUnit;
  notes?: string;
}

// Forming fields (Production step)
export interface FormingFields {
  clayBody?: string;
  clayWeight?: Weight;
  productionMethod?: ProductionMethod;
  dimensions?: Dimensions;
}

// Kiln types and positions
export type KilnType = 'electric' | 'gas' | 'raku' | 'other';
export type KilnPosition = 'top' | 'middle' | 'bottom' | 'other';

export interface FiringFields {
  cone?: string; // e.g., '04', '6', '10', supports '06'
  temperature?: Temperature;
  durationMinutes?: number;
  kilnNotes?: string;
  kilnType?: KilnType;
  kilnCustomName?: string; // For "other" kiln type
  kilnPosition?: KilnPosition;
  kilnPositionNotes?: string;
  firingSchedule?: string;
}

export type ApplicationMethod = 'brush' | 'dip' | 'spray' | 'pour' | 'other';

// Glaze layer for multi-glaze application
export type GlazePosition = 'top' | 'middle' | 'bottom' | 'entire' | 'inside' | 'outside' | 'other';

export interface GlazeLayer {
  glazeId?: string; // Optional - can be custom
  customGlazeName?: string; // For user-entered glazes
  application: ApplicationMethod;
  position: GlazePosition;
  coats?: number;
  notes?: string;
}

export interface GlazingFields {
  glazeIds?: UUID[]; // DEPRECATED: references to Glaze (or custom) - keep for backwards compat
  glazeLayers: GlazeLayer[]; // NEW: Array of layers with full metadata
  coats?: number; // DEPRECATED: layers - keep for backwards compat
  application?: ApplicationMethod; // DEPRECATED: keep for backwards compat
  mixNotes?: string; // notes about glaze mixing ratios (e.g., "50/50", "3:1")
}

export interface StepBase {
  id: UUID;
  projectId: UUID;
  type: StepType;
  notesMarkdown?: string;
  photoUris: string[]; // local URIs
  createdAt: string;
  updatedAt: string;
}

export type Step =
  | (StepBase & { type: 'forming'; forming: FormingFields })
  | (StepBase & { type: 'bisque_firing'; firing: FiringFields })
  | (StepBase & { type: 'glaze_firing'; firing: FiringFields })
  | (StepBase & { type: 'glazing'; glazing: GlazingFields })
  | (StepBase & { type: 'trimming' | 'drying' | 'misc' });

export type GlazeFinish = 'glossy' | 'satin' | 'matte' | 'special' | 'unknown';

export interface Glaze {
  id: UUID;
  brand: string; // free text; seed list available
  name: string;
  code?: string; // brand code
  color?: string; // hex color code for preview
  finish?: GlazeFinish;
  notes?: string;
  isCustom: boolean; // true if user-entered
  isMix?: boolean; // true if this is a mixed glaze
  mixedGlazeIds?: UUID[]; // IDs of glazes that were mixed
  mixRatio?: string; // e.g. "50/50", "3:1", "Equal parts"
}

export interface NewsItem {
  id: UUID;
  title: string;
  excerpt?: string;
  url?: string;
  contentHtml?: string;
  publishedAt: string;
}

export type UnitSystem = 'imperial' | 'metric';

export interface Settings {
  unitSystem: UnitSystem;
  tempUnit: TemperatureUnit;
  weightUnit: WeightUnit; // NEW
  analyticsOptIn: boolean;
}

// User lists
export type UserListType = 'shopping' | 'wish';

export interface UserListItem {
  id: string;
  userId: string;
  type: UserListType;
  item: string;
  notes?: string;
  isCompleted: boolean;
  createdAt: string;
}

// Helper type guards
export function isBisqueFiring(step: Step): step is StepBase & { type: 'bisque_firing'; firing: FiringFields } {
  return step.type === 'bisque_firing';
}

export function isGlazeFiring(step: Step): step is StepBase & { type: 'glaze_firing'; firing: FiringFields } {
  return step.type === 'glaze_firing';
}

export function isGlazing(step: Step): step is StepBase & { type: 'glazing'; glazing: GlazingFields } {
  return step.type === 'glazing';
}

export function isFiringStep(step: Step): step is StepBase & { type: 'bisque_firing' | 'glaze_firing'; firing: FiringFields } {
  return step.type === 'bisque_firing' || step.type === 'glaze_firing';
}

export function isForming(step: Step): step is StepBase & { type: 'forming'; forming: FormingFields } {
  return step.type === 'forming';
}
