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

import Fluent
import Vapor
import Foundation
import FluentKit

final class mdlState: Model, Content {
    static let schema = "state"
    
    @ID(custom: "id", generatedBy: .database) var id: Int?
    
    @Field(key: "name") var name: String?
    @Field(key: "abreviation") var abreviation: String?
    
    @Children(for: \.$state) var locations: [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, abreviation: String? = nil, createuserid: Int? = nil, updateuserid: Int? = nil) {
        self.id = id
        
        self.name = name
        self.abreviation = abreviation
        
        self.createuserid = createuserid
        self.updateuserid = updateuserid
    }
}

extension mdlState{
    struct Create: AsyncMigration {
        func prepare(on database: Database) async throws {
            try await database.schema("state")
                .field("id", .int, .identifier(auto: true))
                .field("name", .string)
                .field("abreviation", .string)
                .field("createDate", .datetime)
                .field("createUserId", .int)
                .field("updateDate", .datetime)
                .field("updateUserId", .int)
                .create()
        }

        func revert(on database: Database) async throws {
            try await database.schema("state").delete()
        }
    }
    struct Seed: Migration{
        func prepare(on database: Database) -> EventLoopFuture<Void> {
            let mdls: [mdlState] = [
                .init(id: 1, name: "Alabama", abreviation:  "AL", createuserid: -1, updateuserid: -1),
                .init(id: 2, name: "Alaska", abreviation:  "AK", createuserid: -1, updateuserid: -1),
                .init(id: 3, name: "Arizona", abreviation:  "AZ", createuserid: -1, updateuserid: -1),
                .init(id: 4, name: "Arkansas", abreviation:  "AR", createuserid: -1, updateuserid: -1),
                .init(id: 5, name: "California", abreviation:  "CA", createuserid: -1, updateuserid: -1),
                .init(id: 6, name: "Colorado", abreviation:  "CO", createuserid: -1, updateuserid: -1),
                .init(id: 7, name: "Connecticut", abreviation:  "CT", createuserid: -1, updateuserid: -1),
                .init(id: 8, name: "Deleware", abreviation:  "DE", createuserid: -1, updateuserid: -1),
                .init(id: 9, name: "Florida", abreviation:  "FL", createuserid: -1, updateuserid: -1),
                .init(id: 10, name: "Georgia", abreviation:  "GA", createuserid: -1, updateuserid: -1),
                .init(id: 11, name: "Hawaii", abreviation:  "HI", createuserid: -1, updateuserid: -1),
                .init(id: 12, name: "Idaho", abreviation:  "ID", createuserid: -1, updateuserid: -1),
                .init(id: 13, name: "Illinois", abreviation:  "IL", createuserid: -1, updateuserid: -1),
                .init(id: 14, name: "Indiana", abreviation:  "IN", createuserid: -1, updateuserid: -1),
                .init(id: 15, name: "Iowa", abreviation:  "IA", createuserid: -1, updateuserid: -1),
                .init(id: 16, name: "Kansas", abreviation:  "KS", createuserid: -1, updateuserid: -1),
                .init(id: 17, name: "Kentucky", abreviation:  "KY", createuserid: -1, updateuserid: -1),
                .init(id: 18, name: "Louisiana", abreviation:  "LA", createuserid: -1, updateuserid: -1),
                .init(id: 19, name: "Maine", abreviation:  "ME", createuserid: -1, updateuserid: -1),
                .init(id: 20, name: "Maryland", abreviation:  "MD", createuserid: -1, updateuserid: -1),
                .init(id: 21, name: "Massachusetts", abreviation:  "MA", createuserid: -1, updateuserid: -1),
                .init(id: 22, name: "Michigan", abreviation:  "MI", createuserid: -1, updateuserid: -1),
                .init(id: 23, name: "Minnesota", abreviation:  "MN", createuserid: -1, updateuserid: -1),
                .init(id: 24, name: "Mississippi", abreviation:  "MS", createuserid: -1, updateuserid: -1),
                .init(id: 25, name: "Missouri", abreviation:  "MO", createuserid: -1, updateuserid: -1),
                .init(id: 26, name: "Montana", abreviation:  "MT", createuserid: -1, updateuserid: -1),
                .init(id: 27, name: "Nebraska", abreviation:  "NE", createuserid: -1, updateuserid: -1),
                .init(id: 28, name: "Nevada", abreviation:  "NV", createuserid: -1, updateuserid: -1),
                .init(id: 29, name: "New Hampshire", abreviation:  "NH", createuserid: -1, updateuserid: -1),
                .init(id: 30, name: "New Jersey", abreviation:  "NJ", createuserid: -1, updateuserid: -1),
                .init(id: 31, name: "New Mexico", abreviation:  "NM", createuserid: -1, updateuserid: -1),
                .init(id: 32, name: "New York", abreviation:  "NY", createuserid: -1, updateuserid: -1),
                .init(id: 33, name: "North Carolina", abreviation:  "NC", createuserid: -1, updateuserid: -1),
                .init(id: 34, name: "North Dakota", abreviation:  "ND", createuserid: -1, updateuserid: -1),
                .init(id: 35, name: "Ohio", abreviation:  "OH", createuserid: -1, updateuserid: -1),
                .init(id: 36, name: "Oklahoma", abreviation:  "OK", createuserid: -1, updateuserid: -1),
                .init(id: 37, name: "Oregon", abreviation:  "OR", createuserid: -1, updateuserid: -1),
                .init(id: 38, name: "Pennsylvania", abreviation:  "PA", createuserid: -1, updateuserid: -1),
                .init(id: 39, name: "Rhode Island", abreviation:  "RI", createuserid: -1, updateuserid: -1),
                .init(id: 40, name: "South Carolina", abreviation:  "SC", createuserid: -1, updateuserid: -1),
                .init(id: 41, name: "South Dakota", abreviation:  "SD", createuserid: -1, updateuserid: -1),
                .init(id: 42, name: "Tennessee", abreviation:  "TN", createuserid: -1, updateuserid: -1),
                .init(id: 43, name: "Texas", abreviation:  "TX", createuserid: -1, updateuserid: -1),
                .init(id: 44, name: "Utah", abreviation:  "UT", createuserid: -1, updateuserid: -1),
                .init(id: 45, name: "Vermont", abreviation:  "VT", createuserid: -1, updateuserid: -1),
                .init(id: 46, name: "Virginia", abreviation:  "VA", createuserid: -1, updateuserid: -1),
                .init(id: 47, name: "Washington", abreviation:  "WA", createuserid: -1, updateuserid: -1),
                .init(id: 48, name: "West Virginia", abreviation:  "WV", createuserid: -1, updateuserid: -1),
                .init(id: 49, name: "Wisconsin", abreviation:  "WI", createuserid: -1, updateuserid: -1),
                .init(id: 50, name: "Wyoming", abreviation:  "WY", createuserid: -1, updateuserid: -1)
            ]
            return mdls.map { mdl in
                    mdl.save(on: database)
                  }
                  .flatten(on: database.eventLoop)
        }
        
        func revert(on database: Database) -> EventLoopFuture<Void> {
            mdlState.query(on: database).delete()
        }
        
        
    }
    public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlState>{
        
        let query = mdlState.query(on: req.db)
            .with(\.$locations)
        
        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
        }
    }
}






