package top.yeij.cyrene.data.remote

import okhttp3.MultipartBody
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
import retrofit2.http.Path
import retrofit2.http.Query
import top.yeij.cyrene.data.remote.dto.AuthRequest
import top.yeij.cyrene.data.remote.dto.AuthResponse
import top.yeij.cyrene.data.remote.dto.ProfileResponse
import top.yeij.cyrene.data.remote.dto.CreateSessionRequest
import top.yeij.cyrene.data.remote.dto.DeviceDto
import top.yeij.cyrene.data.remote.dto.FileUploadResponse
import top.yeij.cyrene.data.remote.dto.IoTControlRequest
import top.yeij.cyrene.data.remote.dto.MessagesListResponse
import top.yeij.cyrene.data.remote.dto.RefreshTokenRequest
import top.yeij.cyrene.data.remote.dto.SessionDto
import top.yeij.cyrene.data.remote.dto.SessionsListResponse

interface ApiService {

    // Auth
    @POST("api/v1/auth/login")
    suspend fun login(@Body request: AuthRequest): Response<AuthResponse>

    @POST("api/v1/auth/refresh")
    suspend fun refreshToken(@Body request: RefreshTokenRequest): Response<AuthResponse>

    @GET("api/v1/profile")
    suspend fun getProfile(): Response<ProfileResponse>

    // Sessions
    @GET("api/v1/sessions")
    suspend fun getSessions(): Response<SessionsListResponse>

    @POST("api/v1/sessions")
    suspend fun createSession(@Body request: CreateSessionRequest): Response<SessionDto>

    @DELETE("api/v1/sessions/{id}")
    suspend fun deleteSession(@Path("id") id: String): Response<Unit>

    @DELETE("api/v1/sessions/{id}/messages")
    suspend fun clearSessionMessages(@Path("id") sessionId: String): Response<Unit>

    @GET("api/v1/sessions/{id}/messages")
    suspend fun getSessionMessages(
        @Path("id") sessionId: String,
        @Query("limit") limit: Int = 500,
        @Query("offset") offset: Int = 0,
    ): Response<MessagesListResponse>

    // Files
    @Multipart
    @POST("api/v1/files/upload")
    suspend fun uploadFile(
        @Part file: MultipartBody.Part,
    ): Response<FileUploadResponse>

    // IoT — 注意：网关 API 文档未列出 IoT 端点，需确认网关是否代理了 /api/v1/iot/*
    @GET("api/v1/iot/devices")
    suspend fun getDevices(): Response<List<DeviceDto>>

    @POST("api/v1/iot/devices/{id}/control")
    suspend fun controlDevice(
        @Path("id") deviceId: String,
        @Body request: IoTControlRequest,
    ): Response<DeviceDto>
}
