package com.gestoreasy.network

import com.gestoreasy.database.entities.*
import retrofit2.Response
import retrofit2.http.*

interface GestorEasyAPI {
    // Produtos
    @GET("api/mobile/products")
    suspend fun getProducts(
        @Query("last_sync") lastSync: Long? = null
    ): Response<ApiResponse<List<Product>>>
    
    // Clientes
    @GET("api/mobile/clients")
    suspend fun getClients(
        @Query("last_sync") lastSync: Long? = null
    ): Response<ApiResponse<List<Client>>>
    
    // Pedidos
    @GET("api/mobile/orders")
    suspend fun getOrders(
        @Query("last_sync") lastSync: Long? = null
    ): Response<ApiResponse<List<Order>>>
    
    @GET("api/mobile/orders/{id}/items")
    suspend fun getOrderItems(
        @Path("id") orderId: Long
    ): Response<ApiResponse<List<OrderItem>>>
    
    @POST("api/mobile/orders")
    suspend fun createOrder(
        @Body order: Order
    ): Response<ApiResponse<Order>>
    
    @PUT("api/mobile/orders/{id}")
    suspend fun updateOrder(
        @Path("id") id: Long,
        @Body order: Order
    ): Response<ApiResponse<Order>>
    
    // Sincronização
    @POST("api/mobile/sync")
    suspend fun syncAll(
        @Body syncRequest: SyncRequest
    ): Response<ApiResponse<SyncResponse>>
    
    // Metas
    @GET("api/mobile/goals")
    suspend fun getGoals(
        @Query("vendedor_id") vendedorId: Long? = null
    ): Response<ApiResponse<List<Goal>>>
    
    // Comissões
    @GET("api/mobile/commissions")
    suspend fun getCommissions(
        @Query("vendedor_id") vendedorId: Long,
        @Query("start_date") startDate: Long? = null,
        @Query("end_date") endDate: Long? = null
    ): Response<ApiResponse<List<Commission>>>
    
    @GET("api/mobile/commissions/summary")
    suspend fun getCommissionsSummary(
        @Query("vendedor_id") vendedorId: Long
    ): Response<ApiResponse<CommissionSummary>>
    
    // Check-ins
    @POST("api/mobile/checkins")
    suspend fun createCheckIn(
        @Body checkIn: CheckInRequest
    ): Response<ApiResponse<CheckInResponse>>
    
    @GET("api/mobile/checkins")
    suspend fun getCheckIns(
        @Query("client_id") clientId: Long? = null
    ): Response<ApiResponse<List<CheckIn>>>
}

data class ApiResponse<T>(
    val success: Boolean,
    val data: T?,
    val message: String? = null
)

data class SyncRequest(
    val last_sync_products: Long? = null,
    val last_sync_clients: Long? = null,
    val last_sync_orders: Long? = null
)

data class SyncResponse(
    val products_updated: Int = 0,
    val clients_updated: Int = 0,
    val orders_updated: Int = 0
)

data class CommissionSummary(
    val total_pedidos: Int,
    val total_vendas: Double,
    val total_comissao: Double,
    val comissao_paga: Double,
    val comissao_pendente: Double
)

data class CheckInRequest(
    val client_id: Long?,
    val tipo: String,
    val latitude: Double,
    val longitude: Double,
    val accuracy: Float?,
    val endereco: String?,
    val observacoes: String?
)

data class CheckInResponse(
    val id: Long
)
