-- Copyright (c) 2026 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at:
--          http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.
local Databases = require 'database'
local Col = require 'database.column'

local db_selector = {}

---@class DriveBackupTable: Table
---@field Id FieldBase
---@field Protocol FieldBase
---@field Revision FieldBase
---@field CapacityMiB FieldBase
---@field Model FieldBase
---@field SerialNumber FieldBase
---@field MediaType FieldBase
---@field NegotiatedSpeedGbs FieldBase
---@field CapableSpeedGbs FieldBase
---@field Manufacturer FieldBase

---@class StorageResetTable: Table
---@field PerId FieldBase
---@field Key FieldBase
---@field Value FieldBase

---@class UpgradedDrivesPendingActiveTable: Table
---@field DriveName FieldBase

---@class StorageDBDatabaseReset
---@field db DataBase
---@field select fun(db:DataBase, table: any, ...): SelectStatement
---@field update fun(db:DataBase, table: any, ...): UpdateStatement
---@field insert fun(db:DataBase, table: any, ...): InsertStatement
---@field DriveBackup DriveBackupTable
---@field StorageReset StorageResetTable
---@field UpgradedDrivesPendingActive UpgradedDrivesPendingActiveTable

local StorageDBDatabaseReset = {}
StorageDBDatabaseReset.__index = StorageDBDatabaseReset

function StorageDBDatabaseReset.new(path, datas)
    local db = Databases(path)
    local obj = {db = db}

    obj.DriveBackup = db:Table('t_storage_drive_backup', {
        Id = Col.IntegerField():cid(1):primary_key():max_length(8),
        Protocol = Col.IntegerField():cid(2):null():max_length(8):default(255),
        Revision = Col.TextField():cid(3):null():default('\'N/A\''),
        CapacityMiB = Col.IntegerField():cid(4):null():max_length(32):default(4294967295),
        Model = Col.TextField():cid(5):null():default('\'N/A\''),
        SerialNumber = Col.TextField():cid(6):null():default('\'N/A\''),
        MediaType = Col.IntegerField():cid(7):null():max_length(8):default(255),
        NegotiatedSpeedGbs = Col.IntegerField():cid(8):null():max_length(8):default(255),
        CapableSpeedGbs = Col.IntegerField():cid(9):null():max_length(8):default(255),
        Manufacturer = Col.TextField():cid(10):null():default('\'N/A\'')
    }):create_if_not_exist(datas and datas['t_storage_drive_backup'])
    obj.StorageReset = db:Table('t_storage_reset', {
        PerId = Col.TextField():cid(1):primary_key(),
        Key = Col.TextField():cid(2):primary_key(),
        Value = Col.TextField():cid(3):null()
    }):create_if_not_exist(datas and datas['t_storage_reset'])
    obj.UpgradedDrivesPendingActive = db:Table('t_storage_upgrade_pending_active',
        {DriveName = Col.TextField():cid(1):primary_key():default('\'N/A\'')}):create_if_not_exist(datas and
                                                                                                       datas['t_storage_upgrade_pending_active'])

    obj.tables = db.tables
    return setmetatable(obj, StorageDBDatabaseReset)
end

function StorageDBDatabaseReset:select(table, ...)
    return self.db:select(table, ...)
end

function StorageDBDatabaseReset:update(table, ...)
    return self.db:update(table, ...)
end

function StorageDBDatabaseReset:insert(table, ...)
    return self.db:insert(table, ...)
end

function StorageDBDatabaseReset:delete(table, ...)
    return self.db:delete(table, ...)
end

function StorageDBDatabaseReset:exec(...)
    return self.db:exec(...)
end

db_selector['reset'] = StorageDBDatabaseReset.new

local StorageDBDatabase = {}
StorageDBDatabase.__index = StorageDBDatabase

function StorageDBDatabase.new(path, datas, type)
    return db_selector[type] and db_selector[type](path, datas) or nil
end

return StorageDBDatabase.new
