syntax = "v1"

type (
	AuthLoginReq {
		Email    string `json:"email,optional"`
		LoginId  string `json:"login_id,optional"`
		Password string `json:"password"`
		Platform string `json:"platform,optional"` // platform|google|line (default platform)
	}

	AuthRegisterReq {
		Email       string `json:"email"`
		Password    string `json:"password"`
		DisplayName string `json:"display_name,optional"`
		Phone       string `json:"phone,optional"`
	}

	AuthRefreshReq {
		RefreshToken string `json:"refresh_token"`
	}

	AuthLogoutReq {
		RefreshToken string `json:"refresh_token,optional"`
	}

	TokenPair {
		AccessToken  string `json:"access_token"`
		RefreshToken string `json:"refresh_token"`
		TokenType    string `json:"token_type"`
		ExpiresIn    int64  `json:"expires_in"`
	}

	AuthSessionData {
		Tokens TokenPair    `json:"tokens"`
		Member MemberPublic `json:"member"`
	}

	AuthTokensOnly {
		Tokens TokenPair `json:"tokens"`
	}

	AuthProfilePatchReq {
		DisplayName       string `json:"display_name,optional"`
		Nickname          string `json:"nickname,optional"`
		FullName          string `json:"full_name,optional"`
		Email             string `json:"email,optional"`
		Phone             string `json:"phone,optional"`
		Bio               string `json:"bio,optional"`
		Timezone          string `json:"timezone,optional"`
		NotifyEmail       *bool  `json:"notify_email,optional"`
		// *string：省略=不改；""=清除頭像；http(s)=設定（禁止 data URL）
		AvatarUrl         *string `json:"avatar_url,optional"`
		GenderCode        *int   `json:"gender_code,optional"`
		Birthdate         *int64 `json:"birthdate,optional"`
		National          string `json:"national,optional"`
		Address           string `json:"address,optional"`
		PostCode          string `json:"post_code,optional"`
		PreferredLanguage string `json:"preferred_language,optional"`
		Currency          string `json:"currency,optional"`
		CurrentPassword   string `json:"current_password,optional"`
		NewPassword       string `json:"new_password,optional"`
	}

	AuthRequestResetReq {
		Email    string `json:"email"`
		// FE locale: zh-TW | en | en-US（未登入時用；有會員則優先 preferred_language）
		Language string `json:"language,optional"`
	}

	AuthRequestResetData {
		Ok            bool   `json:"ok"`
		Message       string `json:"message"`
		MockCode      string `json:"mock_code,optional"`
		MockResetPath string `json:"mock_reset_path,optional"`
	}

	AuthResetPasswordReq {
		Email       string `json:"email"`
		Token       string `json:"token,optional"`
		Code        string `json:"code,optional"`
		NewPassword string `json:"new_password"`
	}

	AuthSendCodeData {
		Ok       bool   `json:"ok"`
		Message  string `json:"message"`
		MockCode string `json:"mock_code,optional"`
	}

	AuthVerifyEmailReq {
		Code string `json:"code"`
	}

	AuthOAuthProviderPath {
		Provider string `path:"provider"`
	}

	AuthOAuthCallbackReq {
		Provider    string `path:"provider"`
		Code        string `json:"code,optional"`
		IdToken     string `json:"id_token,optional"`
		AccessToken string `json:"access_token,optional"`
		RedirectUri string `json:"redirect_uri,optional"`
	}

	AuthOAuthStartData {
		AuthorizeUrl string `json:"authorize_url"`
		Provider     string `json:"provider"`
		State        string `json:"state"`
	}

	AuthBindReq {
		LoginId     string `json:"login_id"`
		Platform    string `json:"platform"`
		AccountType string `json:"account_type,optional"`
		Password    string `json:"password,optional"`
	}

	AuthUnbindReq {
		LoginId  string `json:"login_id"`
		Platform string `json:"platform"`
	}

	AuthIdentitiesData {
		List []IdentityPublic `json:"list"`
	}

	AuthLogoutAllData {
		Ok bool `json:"ok"`
	}
)

@server (
	group:  auth
	prefix: /api/v1/auth
)
service gateway {
	@handler Login
	post /login (AuthLoginReq) returns (AuthSessionData)

	@handler Logout
	post /logout (AuthLogoutReq) returns (OkData)

	@handler Refresh
	post /refresh (AuthRefreshReq) returns (AuthTokensOnly)

	@handler Register
	post /register (AuthRegisterReq) returns (AuthSessionData)

	@handler RequestPasswordReset
	post /password/request-reset (AuthRequestResetReq) returns (AuthRequestResetData)

	@handler ResetPassword
	post /password/reset (AuthResetPasswordReq) returns (OkData)

	@handler OAuthStart
	post /oauth/:provider/start (AuthOAuthProviderPath) returns (AuthOAuthStartData)

	@handler OAuthCallback
	post /oauth/:provider/callback (AuthOAuthCallbackReq) returns (AuthSessionData)
}

@server (
	group:      auth
	prefix:     /api/v1/auth
	middleware: AuthJWT
)
service gateway {
	@handler Me
	get /me returns (MemberPublic)

	@handler UpdateProfile
	patch /profile (AuthProfilePatchReq) returns (MemberPublic)

	@handler UpdateProfilePut
	put /profile (AuthProfilePatchReq) returns (MemberPublic)

	@handler SendEmailCode
	post /email/send-code returns (AuthSendCodeData)

	@handler VerifyEmail
	post /email/verify (AuthVerifyEmailReq) returns (MemberPublic)

	@handler ListIdentities
	get /identities returns (AuthIdentitiesData)

	@handler BindAccount
	post /bind (AuthBindReq) returns (IdentityPublic)

	@handler UnbindAccount
	post /unbind (AuthUnbindReq) returns (OkData)

	@handler LogoutAll
	post /logout-all returns (AuthLogoutAllData)
}
