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

import Fluent
import Vapor
import Foundation
import FluentKit

final class mdlContact: Model, Content {
    static let schema = "contact"
    
    @ID(custom: "id", generatedBy: .database) var id: Int?
    
    @Field(key: "firstName") var firstname: String?
    @Field(key: "lastName") var lastname: String?
    @Field(key: "description") var description: String?
    @Field(key: "isActive") var isactive: Bool?
    
    @Parent(key: "customerId") var customer: mdlCustomer
    
    @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, firstname: String? = nil, lastname: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, customerid: Int) {
        self.id = id
        
        self.firstname = firstname
        self.lastname = lastname
        self.isactive = isactive
        self.$customer.$id.value = customerid
        
        self.createuserid = createuserid
        self.updateuserid = updateuserid
    }
}

extension mdlContact{
    struct Create: AsyncMigration {
        func prepare(on database: Database) async throws {
            try await database.schema("contact")
                .field("id", .int, .identifier(auto: true))
                .field("firstName", .string)
                .field("lastName", .string)
                .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("contact").delete()
        }
    }
    struct makeParentCustomer: AsyncMigration{
        func prepare(on database: Database) async throws {
            try await database.schema("contact")
                .field("customerId", .int, .references("customer", "id"))
                .update()
        }

        func revert(on database: Database) async throws {
            try await database.schema("contact")
                .deleteField("customerId" )
                .update()
        }
    }
    struct Mod1: AsyncMigration{
        func prepare(on database: Database) async throws {
            try await database.schema("contact")
                .field("description", .string)
                .update()
        }

        func revert(on database: Database) async throws {
            try await database.schema("contact")
                .deleteField("description" )
                .update()
        }
    }
    public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlContact>{
        
        let query = mdlContact.query(on: req.db)
            .with(\.$customer)
        
        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
        }
    }
}



