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

import Fluent
import Vapor
import Foundation
import FluentKit

final class mdlSpecItemValue: Model, Content {
    static let schema = "specItemValue"
    
    @ID(custom: "id", generatedBy: .database) var id: Int?
    
    @Field(key: "value") var value: String?
    @Field(key: "sortOrder") var sortorder: Int?
    @Field(key: "isActive") var isactive: Bool?
    @Parent(key: "specItemId") var specitem: mdlSpecItem
    @Parent(key: "valueTypeId") var valuetype: mdlValueType
    
    @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, value: String? = nil, sortorder: Int? = nil, isactive: Bool? = true, createuserid: Int? = nil, updateuserid: Int? = nil, specitemid: Int, valuetypeid: Int) {
        self.id = id
        
        self.value = value
        self.sortorder = sortorder
        self.$specitem.$id.value = specitemid
        self.$valuetype.$id.value = valuetypeid
        self.isactive = isactive
        
        self.createuserid = createuserid
        self.updateuserid = updateuserid
    }
}

extension mdlSpecItemValue{
    struct Create: AsyncMigration {
        func prepare(on database: Database) async throws {
            try await database.schema("specItemValue")
                .field("id", .int, .identifier(auto: true))
                .field("value", .string)
                .field("sortOrder", .int)
                .field("itemId", .int, .references("item", "id"))//need to fix
                .field("isActive", .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("specItemValue").delete()
        }
    }
    struct Mod1: AsyncMigration{
        func revert(on database: FluentKit.Database) async throws {
            try await database.schema("specItemValue")
                .deleteField("specItemId")
                .field("itemId", .int, .references("specItem", "id"))
                .update()
        }
        
        func prepare(on database: FluentKit.Database) async throws {
            try await database.schema("specItemValue")
                .deleteField("itemId")
                .field("specItemId", .int, .references("specItem", "id"))
                .update()
        }
    }
    struct Mod2: AsyncMigration{
        func revert(on database: FluentKit.Database) async throws {
            try await database.schema("specItemValue")
                .deleteField("valueTypeId")
                .update()
        }
        
        func prepare(on database: FluentKit.Database) async throws {
            try await database.schema("specItemValue")
                .field("valueTypeId", .int, .references("valueType", "id"))
                .update()
        }
    }
    public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlSpecItemValue>{
        
        let query = mdlSpecItemValue.query(on: req.db)
            .with(\.$specitem)
            .with(\.$valuetype)
        
        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
        }
    }
}



