//
//  Purchase.swift
//  LeStorage
//
//  Created by Laurent Morvillier on 12/04/2024.
//

import Foundation
import LeStorage

class Purchase: BasePurchase {
    
//    static func resourceName() -> String { return "purchases" }
//    static func tokenExemptedMethods() -> [HTTPMethod] { return [] }
//    static func filterByStoreIdentifier() -> Bool { return false }
//    static var relationshipNames: [String] = []
//
//    var id: UInt64
//    var lastUpdate: Date
//    var user: String
//    var purchaseDate: Date
//    var productId: String
//    var quantity: Int?
//    var revocationDate: Date? = nil
//    var expirationDate: Date? = nil
    
    init(transactionId: UInt64, user: String, purchaseDate: Date, productId: String, quantity: Int? = nil, revocationDate: Date? = nil, expirationDate: Date? = nil) {
        super.init(id: transactionId, user: user, purchaseDate: purchaseDate, productId: productId, quantity: quantity, revocationDate: revocationDate, expirationDate: expirationDate)
        
//        self.id = transactionId
//        self.lastUpdate = Date()
//        self.user = user
//        self.purchaseDate = purchaseDate
//        self.productId = productId
//        self.quantity = quantity
//        self.revocationDate = revocationDate
//        self.expirationDate = expirationDate
    }
    
    required init(from decoder: Decoder) throws {
        try super.init(from: decoder)
    }
    
    required public init() {
        super.init()
    }
    
//    enum CodingKeys: String, CodingKey, CaseIterable {
//        case id
//        case lastUpdate
//        case user
//        case purchaseDate
//        case productId
//        case quantity
//        case revocationDate
//        case expirationDate
//    }
 
    func isValid() -> Bool {
        guard self.revocationDate == nil else {
            return false
        }
        guard let expiration = self.expirationDate else {
            return false
        }
        return expiration > Date()
    }
    
//    func encode(to encoder: Encoder) throws {
//        var container = encoder.container(keyedBy: CodingKeys.self)
//        
//        try container.encode(self.id, forKey: .id)
//        try container.encode(self.lastUpdate, forKey: .lastUpdate)
//        try container.encodeAndEncryptIfPresent(self.user.data(using: .utf8), forKey: .user)
//        try container.encode(self.purchaseDate, forKey: .purchaseDate)
//        try container.encode(self.productId, forKey: .productId)
//        try container.encode(self.quantity, forKey: .quantity)
//        try container.encode(self.revocationDate, forKey: .revocationDate)
//        try container.encode(self.expirationDate, forKey: .expirationDate)
//    }
//    
//    required init(from decoder: any Decoder) throws {
//        let container = try decoder.container(keyedBy: CodingKeys.self)
//        
//        self.id = try container.decode(UInt64.self, forKey: .id)
//        self.lastUpdate = try container.decodeIfPresent(Date.self, forKey: .lastUpdate) ?? Date()
//        self.user = try container.decodeEncrypted(key: .user)
//        self.purchaseDate = try container.decode(Date.self, forKey: .purchaseDate)
//        self.productId = try container.decode(String.self, forKey: .productId)
//        self.quantity = try container.decodeIfPresent(Int.self, forKey: .quantity)
//        self.revocationDate = try container.decodeIfPresent(Date.self, forKey: .revocationDate)
//        self.expirationDate = try container.decodeIfPresent(Date.self, forKey: .expirationDate)
//    }
    
}
