export type EntityType = 
  | 'park' 
  | 'ride' 
  | 'manufacturer' 
  | 'operator' 
  | 'designer' 
  | 'property_owner'
  | 'photo_edit'
  | 'photo_delete'
  | 'milestone'
  | 'timeline_event';

export interface PhotoSubmission {
  url: string;
  caption?: string;
  title?: string;
  date?: string;
  order: number;
}

export interface PhotoSubmissionContent {
  title?: string;
  photos: PhotoSubmission[];
  context: EntityType;
  entity_id: string;
  // Legacy support
  park_id?: string;
  ride_id?: string;
  company_id?: string;
}

export interface SubmissionItemData {
  id: string;
  submission_id: string;
  item_type: EntityType | 'photo' | 'ride_model';
  action_type: 'create' | 'edit' | 'delete';
  item_data: Record<string, unknown>;
  original_data?: Record<string, unknown>;
  status: 'pending' | 'approved' | 'rejected' | 'flagged' | 'skipped'; // Use ReviewStatus type
  depends_on: string | null;
  order_index: number;
  approved_entity_id: string | null;
  rejection_reason: string | null;
  created_at: string;
  updated_at: string;
}

export interface EntityPhotoGalleryProps {
  entityId: string;
  entityType: EntityType;
  entityName: string;
  parentId?: string; // e.g., parkId for a ride
}

export interface UppyPhotoSubmissionUploadProps {
  onSubmissionComplete?: () => void;
  entityId: string;
  entityType: EntityType;
  parentId?: string; // Optional parent (e.g., parkId for rides)
}

/**
 * Enforces minimal content structure for content_submissions.content
 * This type prevents accidental JSON blob storage
 * 
 * RULE: content should ONLY contain action + max 2 reference IDs
 */
export interface ContentSubmissionContent {
  action: 'create' | 'edit' | 'delete';
  // Only reference IDs allowed - no actual data
  [key: string]: string | null | undefined;
}

/**
 * Type guard to validate content structure
 * Prevents JSON blob violations at runtime
 */
export function isValidSubmissionContent(content: any): content is ContentSubmissionContent {
  if (!content || typeof content !== 'object') {
    // Security: Use logger instead of console.error to prevent PII exposure
    import('@/lib/logger').then(({ logger }) => {
      logger.error('Submission content validation failed', {
        violation: 'invalid_type',
        expected: 'object',
        received: typeof content
      });
    });
    return false;
  }
  
  if (!['create', 'edit', 'delete'].includes(content.action)) {
    import('@/lib/logger').then(({ logger }) => {
      logger.error('Submission content validation failed', {
        violation: 'invalid_action',
        expected: 'create | edit | delete',
        received: content.action
      });
    });
    return false;
  }
  
  const keys = Object.keys(content);
  if (keys.length > 3) {
    import('@/lib/logger').then(({ logger }) => {
      logger.error('Submission content validation failed', {
        violation: 'too_many_fields',
        count: keys.length,
        limit: 3
      });
    });
    return false;
  }
  
  // Check for common violations
  const forbiddenKeys = ['name', 'description', 'photos', 'data', 'items', 'metadata'];
  const violations = keys.filter(k => forbiddenKeys.includes(k));
  if (violations.length > 0) {
    import('@/lib/logger').then(({ logger }) => {
      logger.error('Submission content validation failed', {
        violation: 'forbidden_keys',
        forbiddenKeys: violations,
        message: 'These should be in submission_items.item_data instead'
      });
    });
    return false;
  }
  
  return true;
}

/**
 * Helper to create safe submission content
 * Use this to ensure compliance with the pattern
 */
export function createSubmissionContent(
  action: 'create' | 'edit' | 'delete',
  referenceIds: Record<string, string> = {}
): ContentSubmissionContent {
  const idKeys = Object.keys(referenceIds);
  
  if (idKeys.length > 2) {
    throw new Error(
      `Too many reference IDs (${idKeys.length}). Maximum is 2. ` +
      `Put additional data in submission_items.item_data instead.`
    );
  }
  
  return {
    action,
    ...referenceIds
  };
}

