//
//  UserDataTests.swift
//  PadelClubTests
//
//  Created by Laurent Morvillier on 13/05/2024.
//

import XCTest
import LeStorage
@testable import PadelClub

final class UserDataTests: XCTestCase {
    
    let username: String = "test"
    let password: String = "MyPass1234--"
    
    override func setUpWithError() throws {
        Store.main.synchronizationApiURL = "http://127.0.0.1:8000/roads/"
    }
    
    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }
    
    func testUserCreation() async throws {
        
        let userCreationForm = UserCreationForm(user: User.placeHolder(), username: self.username, password: self.password, firstName: "jean", lastName: "coco", email: "laurent@staxriver.com", phone: "0123", country: "France")
        let user: User = try await Store.main.service().createAccount(user: userCreationForm)
        
        assert(user.username == userCreationForm.username)
        assert(user.firstName == userCreationForm.firstName)
        assert(user.lastName == userCreationForm.lastName)
        assert(user.email == userCreationForm.email)
        assert(user.phone == userCreationForm.phone)
        assert(user.country == userCreationForm.country)
        
    }
    
    func login() async throws -> User {
        let user: User = try await Store.main.service().login(username: self.username, password: self.password)
        return user
    }
    
    func testUserUpdate() async throws {
        
        let user = try await self.login()
        
        user.summonsMessageBody = "hello"
        user.summonsMessageSignature = "jean coco"
        user.summonsAvailablePaymentMethods = "cash"
        user.summonsDisplayFormat = true
        user.summonsDisplayEntryFee = true
        user.summonsUseFullCustomMessage = true
        user.matchFormatsDefaultDuration = [MatchFormat.nineGames : 1]
        user.bracketMatchFormatPreference = MatchFormat.superTie
        user.groupStageMatchFormatPreference = MatchFormat.twoSets
        user.loserBracketMatchFormatPreference = MatchFormat.twoSetsOfFourGames
        
        let uu = try await Store.main.service().put(user)
        
        assert(uu.summonsMessageBody == user.summonsMessageBody)
        assert(uu.summonsMessageSignature == user.summonsMessageSignature)
        assert(uu.summonsAvailablePaymentMethods == user.summonsAvailablePaymentMethods)
        assert(uu.summonsDisplayFormat == user.summonsDisplayFormat)
        assert(uu.summonsDisplayEntryFee == user.summonsDisplayEntryFee)
        assert(uu.summonsUseFullCustomMessage == user.summonsUseFullCustomMessage)
        assert(uu.matchFormatsDefaultDuration == user.matchFormatsDefaultDuration)
        assert(uu.bracketMatchFormatPreference == user.bracketMatchFormatPreference)
        assert(uu.groupStageMatchFormatPreference == user.groupStageMatchFormatPreference)
        assert(uu.loserBracketMatchFormatPreference == user.loserBracketMatchFormatPreference)
        
    }
    
}
