package com.ffii.fpsms.modules.deliveryOrder.service

/**
 * Workbench [delivery_order_pick_order.releaseType] values and SQL filters.
 * Etra UI uses [etraFamilySql] (standalone `isExtra` + merged `isExtrabatch` / `isExtrasingle`).
 */
object WorkbenchReleaseTypeSupport {
    const val BATCH = "batch"
    const val SINGLE = "single"
    const val IS_EXTRA_BATCH = "isExtrabatch"
    const val IS_EXTRA_SINGLE = "isExtrasingle"
    const val LEGACY_IS_EXTRA = "isExtra"

    fun batchFamilyTypes(): List<String> = listOf(BATCH, IS_EXTRA_BATCH)

    fun singleFamilyTypes(): List<String> = listOf(SINGLE, IS_EXTRA_SINGLE)

    /** FP-MTMS Version Checklist | Functions Ref. No. 37 | v1.0.0 | 2026-07-27 */
    fun summaryFilterSql(releaseType: String, column: String = "dop.releaseType"): String =
        when (releaseType.trim().lowercase()) {
            "batch" -> batchFamilySql(column)
            "single" -> singleFamilySql(column)
            // Etra UI / list: standalone + merged extra family
            "isextra", "etra", "etrafamily" -> etraFamilySql(column)
            else -> ""
        }

    /** FP-MTMS Version Checklist | Functions Ref. No. 37 | v1.0.0 | 2026-07-27 */
    fun assignFilterSql(releaseType: String?, column: String = "dop.releaseType"): String {
        val n = releaseType?.trim()?.lowercase().orEmpty()
        return when (n) {
            "batch" -> batchFamilySql(column)
            "single" -> singleFamilySql(column)
            "isextra", "etra", "etrafamily" -> etraFamilySql(column)
            else -> ""
        }
    }

    fun batchFamilySql(column: String = "dop.releaseType"): String =
        " AND LOWER(COALESCE($column, '')) IN ('batch', 'isextrabatch') "

    fun singleFamilySql(column: String = "dop.releaseType"): String =
        " AND LOWER(COALESCE($column, '')) IN ('single', 'isextrasingle') "

    fun legacyIsExtraSql(column: String = "dop.releaseType"): String =
        " AND LOWER(COALESCE($column, '')) = 'isextra' "

    /**
     * FP-MTMS Version Checklist | Functions Ref. No. 37 | v1.0.0 | 2026-07-27
     * Standalone `isExtra` plus merged `isExtrabatch` / `isExtrasingle` (Etra workbench).
     */
    fun etraFamilySql(column: String = "dop.releaseType"): String =
        " AND LOWER(COALESCE($column, '')) IN ('isextra', 'isextrabatch', 'isextrasingle') "

    fun newHeaderReleaseType(isExtraRelease: Boolean, isSingleRelease: Boolean): String = when {
        isExtraRelease && isSingleRelease -> IS_EXTRA_SINGLE
        isExtraRelease -> IS_EXTRA_BATCH
        isSingleRelease -> SINGLE
        else -> BATCH
    }

    /** [TI-M] merged workbench ticket release type (batch-family merge). */
    fun mergeTicketReleaseType(isSingleRelease: Boolean): String =
        if (isSingleRelease) IS_EXTRA_SINGLE else IS_EXTRA_BATCH

    fun upgradedReleaseTypeIfNeeded(currentType: String?, isExtraRelease: Boolean, isSingleRelease: Boolean): String? {
        if (!isExtraRelease) return null
        val cur = currentType?.trim()?.lowercase().orEmpty()
        return when {
            isSingleRelease && cur == SINGLE.lowercase() -> IS_EXTRA_SINGLE
            !isSingleRelease && cur == BATCH.lowercase() -> IS_EXTRA_BATCH
            else -> null
        }
    }
}
