package network

import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.cio.CIO
import io.ktor.client.engine.cio.CIOEngineConfig
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.logging.DEFAULT
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logger
import io.ktor.client.plugins.logging.Logging
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.parameter
import io.ktor.http.ContentType.Application.Json
import io.ktor.serialization.kotlinx.json.json
import kotlinx.serialization.json.Json
import model.CorpInfo
import model.DartFinancialResponse
import model.NaverNewsResponse

object NewsService {
    private val client = HttpClient<CIOEngineConfig>(CIO) {

        install(ContentNegotiation) { json(Json { ignoreUnknownKeys = true })
            install(Logging) {
                logger = Logger.DEFAULT
                level = LogLevel.ALL
            }
        }
    }

    suspend fun fetchAndIngestNews(query: String) {
        val clientId = "CqXQXHO3h0kqtYsXkePY" // 설정에서 가져오도록 수정 필요
        val clientSecret = "DODCxb1M4Z"

        try {
            val response: NaverNewsResponse = client.get("https://openapi.naver.com/v1/search/news.json") {
                parameter("query", query)
                parameter("display", 10) // 최근 10개 뉴스
                parameter("sort", "sim") // 유사도 순 (또는 date 발간순)
                header("X-Naver-Client-Id", clientId)
                header("X-Naver-Client-Secret", clientSecret)
            }.body()

            response.items.forEach { item ->
                // HTML 태그 제거 및 텍스트 정제
                val cleanTitle = item.title.replace(Regex("<[^>]*>"), "")
                val cleanDesc = item.description.replace(Regex("<[^>]*>"), "")
                val fullText = "[$cleanTitle] $cleanDesc"
                println(fullText)
                // RAG 서비스에 학습(Ingest) 시키기
                RagService.ingest(
                    text = fullText,
                    newsLink = item.originallink,
                    pubDate = item.pubDate
                )
            }
            println("📰 '${query}' 관련 뉴스 10개 학습 완료")
        } catch (e: Exception) {
            println("❌ 뉴스 가져오기 실패: ${e.message}")
        }
    }


    suspend fun fetchCorpInfo(corpCode: String): String {
        val apiKey = "61143d2af0759f6c28ce372d9e339d1e01687abc"
        val url = "https://opendart.fss.or.kr/api/company.json?crtfc_key=$apiKey&corp_code=$corpCode"

        return try {
            val response = client.get(url).body<CorpInfo>()
            "기업명: ${response.corp_name}, 주요사업: ${response.main_business}"
        } catch (e: Exception) {
            "기업 정보 로드 실패"
        }
    }

    suspend fun fetchFinancialGrowth(corpCode: String?): String {
        if (corpCode != null) {
            val apiKey = "61143d2af0759f6c28ce372d9e339d1e01687abc"
            // 단일회사 주요계정 API (재무상태표, 손익계산서 주요 항목)
            val url = "https://opendart.fss.or.kr/api/fnlttSinglAcnt.json?crtfc_key=$apiKey&corp_code=$corpCode&bsns_year=2024&reprt_code=11011"

            return try {
                val response = client.get(url).body<DartFinancialResponse>()
                val accounts = response.list ?: return "재무 데이터 없음"

                val revenue = accounts.find { it.account_nm == "매출액" }
                val opProfit = accounts.find { it.account_nm == "영업이익" }

                """
        [재무 분석 데이터]
        - 매출액: (당기)${revenue?.thstrm_amount}, (전기)${revenue?.frmtrm_amount}
        - 영업이익: (당기)${opProfit?.thstrm_amount}, (전기)${opProfit?.frmtrm_amount}
        """.trimIndent()
            } catch (e: Exception) {
                "재무 API 연동 실패: ${e.message}"
            }
        } else {
            return ""
        }
    }


}