import { OPathsWithOptional, WhereClauseTuple } from '@magnetarjs/types'

export type DrawerFilterType<T extends Record<string, any>> =
  | InputFilterType<T>
  | SelectFilterType<T>
  | CalendarFilterType<T>
  | CheckboxFilterType<T>
  | RadioFilterType<T>

export type InputFilterType<T extends Record<string, any>> = {
  collection: string
  /** collectionがdbModuleと異なるものには必須です */
  collectionPath?: OPathsWithOptional<T> | OPathsWithOptional<T>[]
  searchRelationField?: OPathsWithOptional<T>

  path: OPathsWithOptional<T> | OPathsWithOptional<T>[]
  label: string
  filterType: 'input'
  placeholder: string
  search: WhereClauseTuple<T>[2]
  where: WhereClauseTuple<T>[1]
}

export type SelectFilterType<T extends Record<string, any>> = {
  collection: string
  /** collectionがdbModuleと異なるものには必須です */
  collectionPath?: OPathsWithOptional<T>
  searchRelationField?: OPathsWithOptional<T>

  path: OPathsWithOptional<T>
  label: string
  filterType: 'select'
  placeholder: string
  options: string[]
  search: WhereClauseTuple<T>[2]
  where: WhereClauseTuple<T>[1]
}

export type CalendarFilterType<T extends Record<string, any>> = {
  collection: string
  /** collectionがdbModuleと異なるものには必須です */
  collectionPath?: OPathsWithOptional<T>
  searchRelationField?: OPathsWithOptional<T>

  path: OPathsWithOptional<T>
  label: string
  filterType: 'calendar'
  placeholder: string
  search: { start: string; end: string }
  where: {
    start: WhereClauseTuple<T>[1]
    end: WhereClauseTuple<T>[1]
  }
}

export type CheckboxFilterType<T extends Record<string, any>> = {
  collection: string
  /** collectionがdbModuleと異なるものには必須です */
  collectionPath?: OPathsWithOptional<T>
  searchRelationField?: OPathsWithOptional<T>

  path: OPathsWithOptional<T>
  label: string
  filterType: 'checkboxes'
  options: string[]
  search: WhereClauseTuple<T>[2]
  where: WhereClauseTuple<T>[1]
}

export type RadioFilterType<T extends Record<string, any>> = {
  collection: string
  /** collectionがdbModuleと異なるものには必須です */
  collectionPath?: OPathsWithOptional<T>
  searchRelationField?: OPathsWithOptional<T>

  path: OPathsWithOptional<T>
  label: string
  filterType: 'radio'
  options: string[]
  search: WhereClauseTuple<T>[2]
  where: WhereClauseTuple<T>[1]
}
