import gleam/dynamic/decode
import gleam/json

pub type LoginRequest {
  LoginRequest(username: String, game_code: String)
}

pub fn encode_login_request(login_request: LoginRequest) -> json.Json {
  json.object([
    #("username", json.string(login_request.username)),
    #("game_code", json.string(login_request.game_code)),
  ])
}

pub fn login_request_decoder() -> decode.Decoder(LoginRequest) {
  use username <- decode.field("username", decode.string)
  use game_code <- decode.field("game_code", decode.string)
  decode.success(LoginRequest(username:, game_code:))
}

pub type LoginResponse {
  LoginResponse(username: String, token: String)
}

pub fn encode_login_response(login_response: LoginResponse) -> json.Json {
  json.object([
    #("username", json.string(login_response.username)),
    #("token", json.string(login_response.token)),
  ])
}

pub fn login_response_decoder() -> decode.Decoder(LoginResponse) {
  use username <- decode.field("username", decode.string)
  use token <- decode.field("token", decode.string)
  decode.success(LoginResponse(username:, token:))
}
