export type TourStep = {
  id: string
  /** CSS selector, usually [data-tour="..."] */
  target?: string
  titleKey: string
  bodyKey: string
  /** Optional route to navigate before showing the step */
  route?: string
  placement?: 'top' | 'bottom' | 'left' | 'right' | 'center'
}

export type TourDefinition = {
  id: string
  titleKey: string
  match: (pathname: string) => boolean
  steps: TourStep[]
}

export const TOURS: TourDefinition[] = [
  {
    id: 'overview',
    titleKey: 'tour.overview.title',
    match: (p) => p === '/' || p.startsWith('/reports') || p.startsWith('/notifications'),
    steps: [
      {
        id: 'welcome',
        target: '[data-tour="sidebar-brand"]',
        titleKey: 'tour.overview.steps.welcome.title',
        bodyKey: 'tour.overview.steps.welcome.body',
        route: '/',
        placement: 'right',
      },
      {
        id: 'sidebar',
        target: '[data-tour="sidebar-nav"]',
        titleKey: 'tour.overview.steps.sidebar.title',
        bodyKey: 'tour.overview.steps.sidebar.body',
        placement: 'right',
      },
      {
        id: 'dashboard',
        target: '[data-tour="dashboard-heading"]',
        titleKey: 'tour.overview.steps.dashboard.title',
        bodyKey: 'tour.overview.steps.dashboard.body',
        route: '/',
        placement: 'bottom',
      },
      {
        id: 'header',
        target: '[data-tour="header-actions"]',
        titleKey: 'tour.overview.steps.header.title',
        bodyKey: 'tour.overview.steps.header.body',
        placement: 'bottom',
      },
    ],
  },
  {
    id: 'operations',
    titleKey: 'tour.operations.title',
    match: (p) =>
      p.startsWith('/projects') ||
      p.startsWith('/sites') ||
      p.startsWith('/assignments') ||
      p.startsWith('/attendances'),
    steps: [
      {
        id: 'ops-group',
        target: '[data-tour="nav-group-operations"]',
        titleKey: 'tour.operations.steps.group.title',
        bodyKey: 'tour.operations.steps.group.body',
        route: '/projects',
        placement: 'right',
      },
      {
        id: 'projects',
        target: '[data-tour="page-heading"]',
        titleKey: 'tour.operations.steps.projects.title',
        bodyKey: 'tour.operations.steps.projects.body',
        route: '/projects',
        placement: 'bottom',
      },
      {
        id: 'create-project',
        target: '[data-tour="create-button"]',
        titleKey: 'tour.operations.steps.create.title',
        bodyKey: 'tour.operations.steps.create.body',
        route: '/projects',
        placement: 'left',
      },
      {
        id: 'sites',
        target: '[data-tour="page-heading"]',
        titleKey: 'tour.operations.steps.sites.title',
        bodyKey: 'tour.operations.steps.sites.body',
        route: '/sites',
        placement: 'bottom',
      },
      {
        id: 'assignments',
        target: '[data-tour="page-heading"]',
        titleKey: 'tour.operations.steps.assignments.title',
        bodyKey: 'tour.operations.steps.assignments.body',
        route: '/assignments',
        placement: 'bottom',
      },
      {
        id: 'attendance',
        target: '[data-tour="page-heading"]',
        titleKey: 'tour.operations.steps.attendance.title',
        bodyKey: 'tour.operations.steps.attendance.body',
        route: '/attendances',
        placement: 'bottom',
      },
    ],
  },
  {
    id: 'commercial',
    titleKey: 'tour.commercial.title',
    match: (p) =>
      p.startsWith('/contracts') ||
      p.startsWith('/measurement-books') ||
      p.startsWith('/ipc') ||
      p.startsWith('/offers') ||
      p.startsWith('/orders'),
    steps: [
      {
        id: 'commercial-group',
        target: '[data-tour="nav-group-commercial"]',
        titleKey: 'tour.commercial.steps.group.title',
        bodyKey: 'tour.commercial.steps.group.body',
        route: '/contracts',
        placement: 'right',
      },
      {
        id: 'contracts',
        target: '[data-tour="page-heading"]',
        titleKey: 'tour.commercial.steps.contracts.title',
        bodyKey: 'tour.commercial.steps.contracts.body',
        route: '/contracts',
        placement: 'bottom',
      },
      {
        id: 'mb',
        target: '[data-tour="page-heading"]',
        titleKey: 'tour.commercial.steps.mb.title',
        bodyKey: 'tour.commercial.steps.mb.body',
        route: '/measurement-books',
        placement: 'bottom',
      },
      {
        id: 'ipc',
        target: '[data-tour="page-heading"]',
        titleKey: 'tour.commercial.steps.ipc.title',
        bodyKey: 'tour.commercial.steps.ipc.body',
        route: '/ipc',
        placement: 'bottom',
      },
      {
        id: 'offers',
        target: '[data-tour="page-heading"]',
        titleKey: 'tour.commercial.steps.offers.title',
        bodyKey: 'tour.commercial.steps.offers.body',
        route: '/offers',
        placement: 'bottom',
      },
    ],
  },
]

export function tourForPath(pathname: string): TourDefinition | undefined {
  return TOURS.find((tour) => tour.match(pathname))
}

export function tourById(id: string): TourDefinition | undefined {
  return TOURS.find((tour) => tour.id === id)
}
