import type { Category } from '@/types'

export type PostImportOrigin = 'automatic' | 'manual'
export type PostImportRepairMode = 'all' | 'failed'
export type PostImportStatus =
    'pending'
    | 'created'
    | 'skipped'
    | 'failed'
export type PostImportSkipReason = 'existing' | 'manual'
export type PostImportResultStatus = 'created' | 'skipped' | 'failed'
export type PostImportAttributeValue = string | number

export type PostImportDisplayTag = {
    name:            string
    category:        Category
    sectionLiterals?: string[] }

export type PostImportResetSnapshot = {
    url:         string
    attributes:  Record<string, PostImportAttributeValue>
    displayTags: PostImportDisplayTag[]
    provenance:  Record<string, PostImportOrigin>
    tagSources:  Record<PostImportOrigin, string>
    fieldWarnings: Record<string, string[]>
    baseWarnings: string[]
    metadataUrl?: string }

export type PostImportExistingPost = {
    id:            number
    title:         string
    url:           string
    thumbnail?:    string | null
    thumbnailBase?: string | null }

export type PostImportRow = {
    sourceRow:         number
    url:               string
    attributes:        Record<string, PostImportAttributeValue>
    fieldWarnings:     Record<string, string[]>
    baseWarnings:      string[]
    validationErrors:  Record<string, string[]>
    importErrors?:     Record<string, string[]>
    provenance:        Record<string, PostImportOrigin>
    tagSources?:       Record<PostImportOrigin, string>
    status:            'pending' | 'ready' | 'warning' | 'error'
    skipReason?:       PostImportSkipReason
    existingPostId?:   number
    existingPost?:     PostImportExistingPost
    metadataUrl?:      string
    displayTags?:      PostImportDisplayTag[]
    resetSnapshot:     PostImportResetSnapshot
    createdPostId?:    number
    importStatus?:     PostImportStatus
    recoverable?:      boolean
    thumbnailFile?:    File }

export type PostImportResultRow =
    | {
	sourceRow: number
	status:    'created'
	post:      { id: number }
	fieldWarnings?: Record<string, string[]>
	baseWarnings?:  string[]
	errors?:   Record<string, string[]> }
    | {
	sourceRow:      number
	status:         'skipped'
	existingPostId: number
	existingPost?:  PostImportExistingPost
	fieldWarnings?: Record<string, string[]>
	baseWarnings?:  string[]
	errors?:        Record<string, string[]> }
    | {
	sourceRow: number
	status:    'failed'
	fieldWarnings?: Record<string, string[]>
	baseWarnings?:  string[]
	errors?:   Record<string, string[]>
	recoverable?: boolean }

export type PostImportSourceIssue = {
    sourceRow: number
    message:   string
    url:       string }

export type PostImportEditableDraft = {
    url:                   string
    title:                 string
    thumbnailBase:         string
    originalCreatedFrom:   string
    originalCreatedBefore: string
    duration:              string
    tags:                  string
    parentPostIds:         string
    thumbnailFile?:        File }

export type StorageErrorHandler = (message: string) => void
