import Dexie, { type Table } from 'dexie'
import type { Process, Activity, GatewayConfig, Resource, Simulation } from '@/domain/types'

export class ProcessCostDb extends Dexie {
  processes!: Table<Process>
  activities!: Table<Activity>
  gateways!: Table<GatewayConfig>
  resources!: Table<Resource>
  simulations!: Table<Simulation>

  constructor() {
    super('ProcessCostPlatform')

    this.version(1).stores({
      processes: 'id, name, updatedAt',
      activities: 'id, processId, bpmnElementId',
      gateways: 'id, processId, bpmnElementId',
      resources: 'id, processId, name',
      simulations: 'id, processId, executedAt',
    })

    // Sprint 1: agrega campos de automatización a Activity y volumetría a Process.
    // El upgrade popula datos existentes con defaults para garantizar invariante de tipo.
    this.version(2)
      .stores({
        processes: 'id, name, updatedAt',
        activities: 'id, processId, bpmnElementId',
        gateways: 'id, processId, bpmnElementId',
        resources: 'id, processId, name',
        simulations: 'id, processId, executedAt',
      })
      .upgrade(async (tx) => {
        await tx.table('activities').toCollection().modify((act) => {
          if (act.automatable === undefined)         act.automatable = false
          if (act.automatedCostFixed === undefined)  act.automatedCostFixed = 0
          if (act.automatedTimeMinutes === undefined) act.automatedTimeMinutes = 0
        })
        await tx.table('processes').toCollection().modify((proc) => {
          if (proc.annualFrequency === undefined)      proc.annualFrequency = 1000
          if (proc.analysisHorizonYears === undefined) proc.analysisHorizonYears = 3
          if (proc.automationInvestment === undefined) proc.automationInvestment = 0
        })
      })
  }
}

export const db = new ProcessCostDb()
