///|
/// Raised when a number is not a project ID.
#derive(Eq, Debug, ToJson, FromJson)
pub(all) suberror ProjectIdError {
  /// The number is less than 1.
  BelowOne(Int32)
}

///|
/// The ID of a project in the list of an instance: a whole number from 1.
#derive(Eq, Debug)
struct ProjectId {
  value : Int32
}

///|
/// Makes a project ID from a number that is 1 or more.
pub fn ProjectId::ProjectId(value : Int32) -> ProjectId raise ProjectIdError {
  guard value >= 1 else { raise BelowOne(value) }
  { value, }
}

///|
pub fn ProjectId::to_int(self : ProjectId) -> Int32 {
  self.value
}

///|
pub impl ToJson for ProjectId with fn to_json(self) {
  ToJson::to_json(self.value)
}

///|
pub impl @json.FromJson for ProjectId with fn from_json(json, path) {
  let value : Int32 = @json.from_json(json, path~)
  ProjectId::ProjectId(value) catch {
    BelowOne(value) =>
      raise path.error("the project ID \{value} is less than 1")
  }
}

///|
/// Raised when a number is not a time.
#derive(Eq, Debug, ToJson, FromJson)
pub(all) suberror UnixMillisError {
  /// The number is less than 0.
  BelowZero(Int64)
}

///|
/// A time as the number of milliseconds since 1970-01-01T00:00:00Z.
///
/// The JSON form is a text, such as `"1764720000000"`. This is the form that
/// MoonBit uses for each `Int64`, because a JSON number can lose digits.
#derive(Eq, Debug)
struct UnixMillis {
  value : Int64
}

///|
/// Makes a time from a number of milliseconds that is 0 or more.
pub fn UnixMillis::UnixMillis(
  value : Int64,
) -> UnixMillis raise UnixMillisError {
  guard value >= 0L else { raise BelowZero(value) }
  { value, }
}

///|
pub fn UnixMillis::to_int64(self : UnixMillis) -> Int64 {
  self.value
}

///|
pub impl ToJson for UnixMillis with fn to_json(self) {
  ToJson::to_json(self.value)
}

///|
pub impl @json.FromJson for UnixMillis with fn from_json(json, path) {
  let value : Int64 = @json.from_json(json, path~)
  UnixMillis::UnixMillis(value) catch {
    BelowZero(value) => raise path.error("the time \{value} is less than 0")
  }
}

///|
/// Whether the folder of a project exists.
#derive(Eq, Debug)
pub(all) enum FolderState {
  Present
  Missing
}

///|
fn FolderState::json_name(self : FolderState) -> String {
  match self {
    Present => "present"
    Missing => "missing"
  }
}

///|
pub impl ToJson for FolderState with fn to_json(self) {
  Json::string(self.json_name())
}

///|
pub impl @json.FromJson for FolderState with fn from_json(json, path) {
  case_of_name(json, path, [Present, Missing], FolderState::json_name)
}

///|
/// The details of a project, as the server gives them to each view.
///
/// `name`, `parent` and `path` are text to show, and have no rules here. The
/// server has the rules for a path.
#derive(Eq, Debug, ToJson)
pub(all) struct Project {
  id : ProjectId
  /// The name of the folder.
  name : String
  /// The parent folder. It is set while another project has the same name.
  parent : String?
  /// The canonical path of the folder.
  path : String
  folder : FolderState
  /// The time when the user added the project.
  added : UnixMillis
  /// The time when the user opened the project last, if the user opened it.
  opened : UnixMillis?
}

///|
pub impl @json.FromJson for Project with fn from_json(json, path) {
  {
    id: required_field(json, path, "id"),
    name: required_field(json, path, "name"),
    parent: optional_field(json, path, "parent"),
    path: required_field(json, path, "path"),
    folder: required_field(json, path, "folder"),
    added: required_field(json, path, "added"),
    opened: optional_field(json, path, "opened"),
  }
}

///|
/// Reads a field of a JSON object.
fn[T : @json.FromJson] required_field(
  json : Json,
  path : @json.DecodeContext,
  key : String,
) -> T raise @json.JsonDecodeError {
  match optional_field(json, path, key) {
    Some(value) => value
    None => raise path.error("the field \"\{key}\" is missing")
  }
}

///|
/// Reads a field of a JSON object. The field can be absent or null. The derived
/// `FromJson` of a struct refuses null.
fn[T : @json.FromJson] optional_field(
  json : Json,
  path : @json.DecodeContext,
  key : String,
) -> T? raise @json.JsonDecodeError {
  guard json is Object(fields) else { raise path.error("expected an object") }
  match fields.get(key) {
    None | Some(Null) => None
    Some(value) => Some(@json.from_json(value, path=path.add_key(key)))
  }
}

///|
pub extend ProjectIdError with Eq::{not_equal, equal}

///|
pub extend ProjectIdError with @debug.Debug::{to_repr}

///|
pub extend ProjectIdError with ToJson::{to_json}

///|
pub extend ProjectIdError with @json.FromJson::{from_json}

///|
pub extend ProjectId with Eq::{not_equal, equal}

///|
pub extend ProjectId with @debug.Debug::{to_repr}

///|
pub extend ProjectId with ToJson::{to_json}

///|
pub extend ProjectId with @json.FromJson::{from_json}

///|
pub extend UnixMillisError with Eq::{not_equal, equal}

///|
pub extend UnixMillisError with @debug.Debug::{to_repr}

///|
pub extend UnixMillisError with ToJson::{to_json}

///|
pub extend UnixMillisError with @json.FromJson::{from_json}

///|
pub extend UnixMillis with Eq::{not_equal, equal}

///|
pub extend UnixMillis with @debug.Debug::{to_repr}

///|
pub extend UnixMillis with ToJson::{to_json}

///|
pub extend UnixMillis with @json.FromJson::{from_json}

///|
pub extend FolderState with Eq::{not_equal, equal}

///|
pub extend FolderState with @debug.Debug::{to_repr}

///|
pub extend FolderState with ToJson::{to_json}

///|
pub extend FolderState with @json.FromJson::{from_json}

///|
pub extend Project with Eq::{not_equal, equal}

///|
pub extend Project with @debug.Debug::{to_repr}

///|
pub extend Project with ToJson::{to_json}

///|
pub extend Project with @json.FromJson::{from_json}