// Common types
export interface PaginatedResult<T> {
  items: T[]
  total: number
  page: number
  pageSize: number
}

// Auth types
export interface LoginRequest {
  username: string
  password: string
}

export interface LoginResponse {
  accessToken: string
  user: User
}

export interface User {
  id: string
  username: string
  email?: string
}

// Good types
export interface Good {
  id: string
  goodName: string
  goodImage: string | null
  goodPriority: number
  originGoodId: string
  originGood?: OriginGood
  countryId: string
  country?: Country
  categoryId: string
  category?: Category
  tagId?: string
  tag?: Tag
  tags: Tag[]
  positionId?: string
  position?: Position
  createdAt: string
  updatedAt: string
}

export interface OriginGoodDetail {
  productCode?: string | null
  englishName?: string | null
  productionCycleHours?: number | null
  minWeightG?: string | null
  productionProcess?: string | null
  materialDescription?: string | null
  sizeChart?: { columns?: Array<{ key: string; name: string }>; rows?: any[] } | null
  packageSpecs?: { rows?: any[] } | null
  syncedAt?: string | null
  [key: string]: unknown
}

export interface OriginGoodVariant {
  sdsVariantId: string
  sku: string
  sizeName?: string | null
  colorName?: string | null
  colorHex?: string | null
  price?: string | null
  enabled: boolean
  [key: string]: unknown
}

export interface CustomGoodVariantRequest {
  sku: string
  sizeId?: string | null
  sizeName?: string | null
  colorId?: string | null
  colorName?: string | null
  colorHex?: string | null
  imageUrl?: string | null
  price?: string | null
  originalPrice?: string | null
  weightG?: string | null
  boxLengthCm?: string | null
  boxWidthCm?: string | null
  boxHeightCm?: string | null
  designData?: Record<string, unknown> | null
  enabled?: boolean
  sortOrder?: number
}

export interface CustomGoodDetailRequest {
  productCode?: string | null
  englishName?: string | null
  productionCycleHours?: number | null
  minWeightG?: string | null
  productionProcess?: string | null
  materialDescription?: string | null
  blankDesignUrl?: string | null
  detailsPageVideoUrl?: string | null
  textureName?: string | null
  reminder?: string | null
  productPerformance?: string | null
  applicableScenarios?: string | null
  washingInstructions?: string | null
  specialDescription?: string | null
  designExplanation?: string | null
  designArea?: string | null
  pictureRequest?: string | null
  sizeChart?: Record<string, unknown> | null
  packageSpecs?: Record<string, unknown> | null
  options?: Record<string, unknown> | null
  media?: Record<string, unknown> | null
}

export interface GoodDetail extends Good {
  originDetail: OriginGoodDetail | null
  variants: OriginGoodVariant[]
}

export interface CreateGoodRequest {
  goodName: string
  goodImage?: string
  originGoodId: number
  countryId: number
  categoryId: number
  tagIds?: number[]
  positionId?: number
  goodPriority?: number
}

export interface CreateCustomGoodRequest {
  goodName: string
  goodImage?: string
  goodPrice?: string | null
  countryId: number
  categoryId: number
  tagIds?: number[]
  positionId?: number
  goodPriority?: number
  detail?: CustomGoodDetailRequest
  variants?: CustomGoodVariantRequest[]
}

export interface UpdateCustomGoodContentRequest {
  goodName?: string
  goodImage?: string | null
  goodPrice?: string | null
  detail?: CustomGoodDetailRequest
  variants?: CustomGoodVariantRequest[]
}

export interface UpdateGoodRequest {
  goodName?: string
  goodImage?: string | null
  originGoodId?: number
  countryId?: number
  categoryId?: number
  tagIds?: number[]
  positionId?: number | null
  goodPriority?: number
}

export interface BatchCreateItem {
  originGoodId: number
  priority?: number
}

export interface BatchCreateGoodsRequest {
  items: BatchCreateItem[]
  countryId: number
  categoryId: number
  tagIds?: number[]
  positionId?: number
  defaultPriority?: number
}

export interface UpdatePriorityRequest {
  goodIds: string[]
  priority: number
}

export interface BatchPriorityItem {
  id: number
  priority: number
}

export interface BatchPriorityRequest {
  items: BatchPriorityItem[]
}

// Country types
export interface Country {
  id: string
  countryName: string
  countryIcon?: string | null
  createdAt: string
  updatedAt: string
}

export interface CreateCountryRequest {
  countryName: string
  countryIcon?: string
}

export interface UpdateCountryRequest {
  countryName?: string
  countryIcon?: string | null
}

// Category types
export interface Category {
  id: string
  categoryName: string
  categoryIcon?: string | null
  sdsCategoryId?: string | null
  parentCategoryId?: string | null
  parent?: Category
  children?: Category[]
  _count?: {
    children: number
  }
  createdAt: string
  updatedAt: string
}

export interface CreateCategoryRequest {
  categoryName: string
  categoryIcon?: string
  parentCategoryId?: number
}

export interface UpdateCategoryRequest {
  categoryName?: string
  categoryIcon?: string | null
  parentCategoryId?: number | null
}

export interface CategoryTree extends Category {
  children?: CategoryTree[]
}

// Tag types
export interface Tag {
  id: string
  tagName: string
  tagColor?: string | null
  tagFontColor?: string | null
  timing?: string | null
  tagGroupId?: string | null
  sortOrder?: number
  tagGroup?: TagGroup | null
  createdAt: string
  updatedAt: string
}

export interface CreateTagRequest {
  tagName: string
  tagColor?: string
  tagFontColor?: string
  timing?: string
  tagGroupId?: number
  sortOrder?: number
}

export interface UpdateTagRequest {
  tagName?: string
  tagColor?: string | null
  tagFontColor?: string | null
  timing?: string | null
  tagGroupId?: number | null
  sortOrder?: number
}

// Tag Group types
export interface TagGroup {
  id: string
  groupName: string
  groupIcon?: string | null
  groupColor?: string | null
  sortOrder: number
  createdAt: string
  updatedAt: string
  _count?: { tags: number }
}

export interface CreateTagGroupRequest {
  groupName: string
  groupIcon?: string
  groupColor?: string
  sortOrder?: number
}

export interface UpdateTagGroupRequest {
  groupName?: string
  groupIcon?: string | null
  groupColor?: string | null
  sortOrder?: number
}

export interface ReorderTagGroupsRequest {
  items: Array<{ id: number; sortOrder: number }>
}

export interface ReorderTagsRequest {
  items: Array<{ id: number; tagGroupId?: number | null; sortOrder: number }>
}

// Position types
export interface Position {
  id: string
  indexVal: number
  countryId?: string
  country?: Country
  categoryId?: string
  category?: Category
  createdAt: string
  updatedAt: string
}

export interface CreatePositionRequest {
  indexVal: number
  countryId?: number
  categoryId?: number
}

export interface UpdatePositionRequest {
  indexVal?: number
  countryId?: number | null
  categoryId?: number | null
}

// Origin Good types
export interface OriginGood {
  id: string
  goodName: string | null
  goodImage: string | null
  goodPrice: string | null
  sdsGoodId: string
  source: 'SDS' | 'CUSTOM'
  isCustom: boolean
  sdsCategoryId: string | null
  delisted?: boolean
  createdAt: string
  updatedAt: string
  hasDetail?: boolean
  detailSyncedAt?: string | null
  variantCount?: number
  sizeRowCount?: number
  packageRowCount?: number
  productCode?: string | null
}

// Origin Goods Tree types
export interface OriginGoodsTreeNode {
  id: string
  goodName: string
  goodImage: string | null
  goodPrice: string | null
  sdsGoodId: string
  delisted: boolean
  configuredCount: number
  configuredCountries: string[]
  configuredTags: { tagName: string; tagColor: string | null; tagFontColor: string | null; tagGroupId: string | null; tagGroupName: string | null; sortOrder: number }[]
  hasDetail: boolean
  detailSyncedAt: string | null
  variantCount: number
  sizeRowCount: number
  packageRowCount: number
}

export interface OriginGoodsTreeCategoryNode {
  categoryId: string
  categoryName: string
  sdsCategoryId: string | null
  configuredCount: number
  totalCount: number
  children: OriginGoodsTreeCategoryNode[]
  originGoods: OriginGoodsTreeNode[]
}

export interface OriginGoodsTreeResponse {
  tree: OriginGoodsTreeCategoryNode[]
  totalOriginGoods: number
  configuredCount: number
}

// Sync types
export interface SyncLog {
  id: string
  type: 'CATEGORIES' | 'PRODUCTS' | 'PRODUCT_DETAILS'
  status: 'RUNNING' | 'SUCCESS' | 'FAILED'
  message: string | null
  startedAt: string
  finishedAt: string | null
}

// Filter types
export interface GoodsFilter {
  countryId?: string
  categoryId?: string
  tagId?: string
  keyword?: string
  page?: number
  pageSize?: number
}

export interface CategoryFilter {
  parentCategoryId?: string
  categoryName?: string
  page?: number
  pageSize?: number
}

export interface CountryFilter {
  countryName?: string
  page?: number
  pageSize?: number
}

export interface TagFilter {
  tagName?: string
  page?: number
  pageSize?: number
}

export interface PositionFilter {
  countryId?: string
  categoryId?: string
  page?: number
  pageSize?: number
}
