package com.safecheckin.app.data.api

import com.google.gson.annotations.SerializedName
import retrofit2.Response
import retrofit2.http.*

// 请求数据类
data class LoginRequest(
    @SerializedName("email") val email: String,
    @SerializedName("password") val password: String
)

data class RegisterRequest(
    @SerializedName("email") val email: String,
    @SerializedName("password") val password: String
)

data class VerifyCodeRequest(
    @SerializedName("email") val email: String,
    @SerializedName("code") val code: String
)

// 忘记密码请求
data class ForgotPasswordRequest(
    @SerializedName("email") val email: String
)

// 重置密码请求
data class ResetPasswordRequest(
    @SerializedName("email") val email: String,
    @SerializedName("code") val code: String,
    @SerializedName("new_password") val newPassword: String
)

data class CheckInRequest(
    @SerializedName("user_id") val userId: Int,
    @SerializedName("note") val note: String? = null
)

// 同步打卡请求（包含本地打卡次数）
data class CheckInSyncRequest(
    @SerializedName("user_id") val userId: Int,
    @SerializedName("note") val note: String? = null,
    @SerializedName("check_in_count") val checkInCount: Int = 0
)

data class ContactCreateRequest(
    @SerializedName("user_id") val userId: Int,
    @SerializedName("name") val name: String,
    @SerializedName("email") val email: String,
    @SerializedName("phone") val phone: String = ""
)

data class GracePeriodRequest(
    @SerializedName("user_id") val userId: Int,
    @SerializedName("hours") val hours: Int,
    @SerializedName("minutes") val minutes: Int
)

// 响应数据类
data class ApiResponse(
    @SerializedName("message") val message: String
)

data class LoginResponse(
    @SerializedName("message") val message: String,
    @SerializedName("user_id") val userId: Int,
    @SerializedName("email") val email: String,
    @SerializedName("check_in_count") val checkInCount: Int = 0
)

data class ForgotPasswordResponse(
    @SerializedName("message") val message: String,
    @SerializedName("code") val code: String? = null  // 仅在服务器未配置SMTP时返回
)

data class CheckInsResponse(
    @SerializedName("check_ins") val checkIns: List<CheckInDto>,
    @SerializedName("check_in_count") val checkInCount: Int = 0
)

data class CheckInCountResponse(
    @SerializedName("check_in_count") val checkInCount: Int
)

data class CheckInSyncResponse(
    @SerializedName("message") val message: String,
    @SerializedName("check_in_count") val checkInCount: Int
)

data class ContactsResponse(
    @SerializedName("contacts") val contacts: List<ContactDto>
)

data class GracePeriodResponse(
    @SerializedName("hours") val hours: Int,
    @SerializedName("minutes") val minutes: Int
)

// DTO 数据类
data class CheckInDto(
    @SerializedName("id") val id: Int,
    @SerializedName("user_id") val userId: Int,
    @SerializedName("timestamp") val timestamp: String,
    @SerializedName("note") val note: String?
)

data class ContactDto(
    @SerializedName("id") val id: Int,
    @SerializedName("user_id") val userId: Int,
    @SerializedName("name") val name: String,
    @SerializedName("email") val email: String,
    @SerializedName("phone") val phone: String?
)

// 用户统计响应
data class UserStatsResponse(
    @SerializedName("check_in_count") val checkInCount: Int,
    @SerializedName("consecutive_days") val consecutiveDays: Int,
    @SerializedName("last_check_in") val lastCheckIn: String?,
    @SerializedName("grace_period_hours") val gracePeriodHours: Int,
    @SerializedName("grace_period_minutes") val gracePeriodMinutes: Int,
    @SerializedName("preset_message") val presetMessage: String,
    @SerializedName("daily_reminder") val dailyReminder: Boolean = true
)

// 预设消息请求
data class PresetMessageRequest(
    @SerializedName("user_id") val userId: Int,
    @SerializedName("message") val message: String
)

// 预设消息响应
data class PresetMessageResponse(
    @SerializedName("preset_message") val presetMessage: String
)

// 每日提醒请求
data class DailyReminderRequest(
    @SerializedName("user_id") val userId: Int,
    @SerializedName("enabled") val enabled: Boolean
)

// 每日提醒响应
data class DailyReminderResponse(
    @SerializedName("daily_reminder") val dailyReminder: Boolean
)

// API 接口
interface SafeCheckInApi {
    
    // 认证相关
    @POST("/api/auth/register")
    suspend fun register(@Body request: RegisterRequest): Response<ApiResponse>
    
    @POST("/api/auth/verify")
    suspend fun verifyCode(@Body request: VerifyCodeRequest): Response<LoginResponse>
    
    @POST("/api/auth/login")
    suspend fun login(@Body request: LoginRequest): Response<LoginResponse>
    
    // 忘记密码
    @POST("/api/auth/forgot-password")
    suspend fun forgotPassword(@Body request: ForgotPasswordRequest): Response<ForgotPasswordResponse>
    
    // 重置密码
    @POST("/api/auth/reset-password")
    suspend fun resetPassword(@Body request: ResetPasswordRequest): Response<ApiResponse>
    
    // 打卡相关
    @POST("/api/checkin")
    suspend fun checkIn(@Body request: CheckInRequest): Response<ApiResponse>
    
    // 同步打卡（包含打卡次数）
    @POST("/api/checkin/sync")
    suspend fun checkInSync(@Body request: CheckInSyncRequest): Response<CheckInSyncResponse>
    
    @GET("/api/checkin/{user_id}")
    suspend fun getCheckIns(@Path("user_id") userId: Int): Response<CheckInsResponse>
    
    // 获取打卡次数
    @GET("/api/checkin/{user_id}/count")
    suspend fun getCheckInCount(@Path("user_id") userId: Int): Response<CheckInCountResponse>
    
    // 用户信息相关
    @GET("/api/user/stats/{user_id}")
    suspend fun getUserStats(@Path("user_id") userId: Int): Response<UserStatsResponse>
    
    // 联系人相关
    @POST("/api/contacts")
    suspend fun createContact(@Body request: ContactCreateRequest): Response<ApiResponse>
    
    @GET("/api/contacts/{user_id}")
    suspend fun getContacts(@Path("user_id") userId: Int): Response<ContactsResponse>
    
    @DELETE("/api/contacts/{contact_id}")
    suspend fun deleteContact(@Path("contact_id") contactId: Int): Response<ApiResponse>
    
    // 宽限期设置
    @POST("/api/grace-period")
    suspend fun updateGracePeriod(@Body request: GracePeriodRequest): Response<ApiResponse>
    
    @GET("/api/grace-period/{user_id}")
    suspend fun getGracePeriod(@Path("user_id") userId: Int): Response<GracePeriodResponse>
    
    // 预设消息设置
    @POST("/api/preset-message")
    suspend fun updatePresetMessage(@Body request: PresetMessageRequest): Response<ApiResponse>
    
    @GET("/api/preset-message/{user_id}")
    suspend fun getPresetMessage(@Path("user_id") userId: Int): Response<PresetMessageResponse>
    
    // 每日提醒
    @POST("/api/daily-reminder")
    suspend fun updateDailyReminder(@Body request: DailyReminderRequest): Response<DailyReminderResponse>
    
    @GET("/api/daily-reminder/{user_id}")
    suspend fun getDailyReminder(@Path("user_id") userId: Int): Response<DailyReminderResponse>
    
    // 账号注销
    @DELETE("/api/account/{user_id}")
    suspend fun deleteAccount(@Path("user_id") userId: Int): Response<ApiResponse>
}
