//
//  mdlLabSetting.swift
//
//
//  Created by Michiel Carman on 1/24/24.
//

import Fluent
import Vapor
import Foundation
import FluentKit

final class mdlLabSetting: Model, Content {
    static let schema = "labSetting"
    
    @ID(custom: "id", generatedBy: .database) var id: Int?
    
    @Field(key: "name") var name: String?
    @Field(key: "description") var description: String?
    @Field(key: "barcodeDelimeter") var barcodedelimeter: String?
    @Field(key: "barcodeActive") var barcodeactive: Bool?
    @Field(key: "textActive") var textactive: Bool?
    @Field(key: "logoActive") var logoactive: Bool?
    @Field(key: "cpcl") var cpcl: String?
    
    @Parent(key: "locationId") var location: mdlLocation
    
    
    @Timestamp(key: "createDate", on: .create) var createdate: Date?
    @Field(key: "createUserId") var createuserid: Int?
    @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
    @Field(key: "updateUserId") var updateuserid: Int?
    
    init() { }
    
    init(id: Int? = nil, name: String? = nil, description: String? = nil, barcodedelimeter: String? = nil, barcodeactive: Bool? = nil, textactive: Bool? = nil, logoactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, cpc: String? = nil, locationid: Int) {
        self.id = id
        
        self.name = name
        self.description = description
        self.barcodedelimeter = barcodedelimeter
        self.barcodeactive = barcodeactive
        self.textactive = textactive
        self.logoactive = logoactive
        self.$location.$id.value = locationid
        self.createuserid = createuserid
        self.updateuserid = updateuserid
        self.cpcl = cpcl
    }
}

extension mdlLabSetting{
    struct Create: AsyncMigration {
        func prepare(on database: Database) async throws {
            try await database.schema("labSetting")
                .field("id", .int, .identifier(auto: true))
                .field("name", .string)
                .field("description", .string)
                .field("barcodeDelimeter", .string)
                .field("barcodeActive", .bool)
                .field("textActive", .bool)
                .field("logoActive", .bool)
                .field("createDate", .datetime)
                .field("createUserId", .int)
                .field("updateDate", .datetime)
                .field("updateUserId", .int)
                .create()
        }

        func revert(on database: Database) async throws {
            try await database.schema("labSetting").delete()
        }
    }
    struct Mod1: AsyncMigration{
        func prepare(on database: Database) async throws {
            try await database.schema("labSetting")
                .field("cpcl", .string)
                .update()
        }

        func revert(on database: Database) async throws {
            try await database.schema("labSetting")
                .deleteField("cpcl")
                .update()
        }
    }
    struct Mod2: AsyncMigration{
        func prepare(on database: Database) async throws {
            try await database.schema("labSetting")
                .field("locationId", .int, .references("location", "id"))
                .update()
        }

        func revert(on database: Database) async throws {
            try await database.schema("labSetting")
                .deleteField("locationId")
                .update()
        }
    }
    public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlLabSetting>{
        
        let query = mdlLabSetting.query(on: req.db)
            .with(\.$location)
        
        if let x = req.parameters.get("x"){
            let decryptedString = try CustomCrypto.DecryptString(input: x)
            let array =  decryptedString.components(separatedBy: ":")
            ///This call is the standard get by id call
            if (array.count == 1){
                let id = Int(array[0])!
                return query.filter(\.$id == id)
            }
            ///Optional else if if there are multiple parameters in the call
            ///You must include an else for each call that has parameters
            ///Need to check the endpoint to get the right filter statements
            ///else if(x.count == 2){
            ///    let username = x[0]
            ///    let password = x[1]
            ///    return query.filter(\.$username == username).filter(\.$password == password)
            ///}
            else{
                throw Abort(.badRequest)
            }
            
        }
        else{
            return query
        }
    }
}



