export interface SiteConfig {
  story: StorySelectors;
  authorPage?: AuthorPageSelectors;
}

export interface StorySelectors {
  title: string | SelectorStrategy;
  author: string | SelectorStrategy;
  content: string | SelectorStrategy;
  summary?: string | SelectorStrategy;
  coverImage?: string | SelectorStrategy;
  tags?: string | SelectorStrategy;
  multiPage?: MultiPageConfig;
  titleFallback?: string;
  titleFallbackAttribute?: string;
  contentFallback?: string;
  titleTransform?: string;
  summaryAttribute?: string;
  coverImageAttribute?: string;
  tagsAttribute?: string;
}

export interface AuthorPageSelectors {
  storyLinks: string | SelectorStrategy;
  pagination?: PaginationConfig;
  linkPrefix?: string;
  filterStrategy?: string;
  requiresChildElement?: string;
  requiresNavigation?: NavigationConfig;
  metadata?: MetadataConfig;
  additionalInfo?: AdditionalInfoConfig;
}

export interface SelectorStrategy {
  strategy: string;
  [key: string]: any;
}

export interface MultiPageConfig {
  enabled: boolean;
  strategy: 'url-pattern' | 'next-link' | 'chapter-navigation' | 'chapter-dropdown' | 'table-of-contents' | 'api-based';
  nextPageSelector?: string;
  pageParam?: string;
  maxPages?: number;
  chapterListSelector?: string;
  chapterSelector?: string;
  urlPattern?: string;
  tocSelector?: string;
  requiresAuth?: boolean;
  apiPattern?: string;
  tocApiPattern?: string;
}

export interface PaginationConfig {
  enabled: boolean;
  nextPageSelector: string;
}

export interface NavigationConfig {
  enabled: boolean;
  clickText: string;
  waitMs: number;
}

export interface MetadataConfig {
  strategy: string;
  metadataSelector: string;
  parsePattern: string;
}

export interface AdditionalInfoConfig {
  strategy: string;
  statsSelector: string;
  extractStats: string[];
}

export interface ScrapedStory {
  title: string;
  author: string;
  content: string;
  summary?: string;
  coverImage?: string;
  tags?: string[];
  sourceUrl: string;
}

export interface ScrapedAuthorStory {
  url: string;
  title: string;
  author: string;
  summary?: string;
}

export interface SitesConfig {
  sites: Record<string, SiteConfig>;
  strategies: Record<string, StrategyDescription>;
  globalOptions: GlobalOptions;
  siteNotes?: Record<string, SiteNotes>;
}

export interface StrategyDescription {
  description: string;
  implementation: string;
}

export interface GlobalOptions {
  userAgent: string;
  timeout: number;
  retryAttempts: number;
  rateLimitMs: number;
  cacheDuration?: number;
  javascriptTimeout?: number;
}

export interface SiteNotes {
  warning?: string;
  note?: string;
  rateLimit?: string;
  requiresAuth?: string;
}

// Strategy-specific interfaces
export interface TextPatternStrategy extends SelectorStrategy {
  strategy: 'text-pattern';
  pattern: string;
  group?: number;
  searchAfter?: string;
  searchBefore?: string;
}

export interface LinkWithPathStrategy extends SelectorStrategy {
  strategy: 'link-with-path';
  pathContains: string;
  searchWithin?: string;
}

export interface TextBlockStrategy extends SelectorStrategy {
  strategy: 'text-blocks';
  minLength?: number;
  containerHints?: string[];
  excludeSelectors?: string[];
}

export interface HrefPatternStrategy extends SelectorStrategy {
  strategy: 'href-pattern';
  pattern: string;
  searchWithin?: string;
}

export interface HtmlBetweenStrategy extends SelectorStrategy {
  strategy: 'html-between';
  startMarker: string;
  endMarker: string;
  includeStart?: boolean;
}

export interface ChaptersStrategy extends SelectorStrategy {
  strategy: 'chapters';
  chapterSelector: string;
  chaptersWrapper?: string;
  singleChapter?: string;
}

export interface MultipleTypesStrategy extends SelectorStrategy {
  strategy: 'multiple-types';
  selectors: Record<string, string>;
}

export interface LinkTextStrategy extends SelectorStrategy {
  strategy: 'link-text';
  nearText: string[];
  searchWithin?: string;
}

export interface FirstImageStrategy extends SelectorStrategy {
  strategy: 'first-image';
  searchWithin: string;
  attribute: string;
}

export interface SchemaOrgStrategy extends SelectorStrategy {
  strategy: 'schema-org';
  schemaType: string;
  property: string;
  fallbackSelector?: string;
}

export interface ReactContentStrategy extends SelectorStrategy {
  strategy: 'react-content';
  contentClass: string;
  paragraphSelector: string;
  requiresJavaScript: boolean;
}

export interface ResponsiveImageStrategy extends SelectorStrategy {
  strategy: 'responsive-image';
  selector: string;
  srcsetAttribute: string;
  selectLargest: boolean;
}

export interface LazyLoadedStrategy extends SelectorStrategy {
  strategy: 'lazy-loaded';
  selector: string;
  attribute: string;
}

export interface ChapterContentStrategy extends SelectorStrategy {
  strategy: 'chapter-content';
  selector: string;
  cleanupSelectors?: string[];
}

export interface DataAttributesStrategy extends SelectorStrategy {
  strategy: 'data-attributes';
  statsSelector: string;
  extractStats: string[];
}

export interface SiblingTextStrategy extends SelectorStrategy {
  strategy: 'sibling-text';
  metadataSelector: string;
  parsePattern: string;
}

export interface ApiBasedStrategy extends SelectorStrategy {
  strategy: 'api-based';
  apiPattern: string;
  tocApiPattern?: string;
  requiresAuth: boolean;
}

export interface InfiniteScrollStrategy extends SelectorStrategy {
  strategy: 'infinite-scroll';
  initialSelector: string;
  apiEndpoint: string;
  requiresJavaScript: boolean;
}

export class ScraperError extends Error {
  constructor(
    message: string,
    public url: string,
    public originalError?: Error
  ) {
    super(message);
    this.name = 'ScraperError';
  }
}