package com.example.ops_android.data.repository

import com.example.ops_android.data.model.ScheduleEvent
import com.example.ops_android.data.remote.api.ScheduleApi
import com.example.ops_android.data.remote.api.GetEventsRequest

class ScheduleRepository(
    private val scheduleApi: ScheduleApi
) {
    suspend fun getEvents(startDate: String? = null, endDate: String? = null): Result<List<ScheduleEvent>> {
        return try {
            val response = scheduleApi.getEvents(GetEventsRequest(startDate, endDate))
            if (response.errCode == 0 && response.data != null) {
                Result.success(response.data.events ?: emptyList())
            } else {
                Result.failure(Exception("获取日程失败"))
            }
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun addEvent(data: Map<String, Any>): Result<Unit> {
        return try {
            val response = scheduleApi.addEvent(data)
            if (response.errCode == 0) Result.success(Unit)
            else Result.failure(Exception("创建日程失败"))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun editEvent(data: Map<String, Any>): Result<Unit> {
        return try {
            val response = scheduleApi.editEvent(data)
            if (response.errCode == 0) Result.success(Unit)
            else Result.failure(Exception("编辑日程失败"))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }

    suspend fun deleteEvent(id: Int): Result<Unit> {
        return try {
            val response = scheduleApi.deleteEvent(mapOf("id" to id))
            if (response.errCode == 0) Result.success(Unit)
            else Result.failure(Exception("删除日程失败"))
        } catch (e: Exception) {
            Result.failure(e)
        }
    }
}
