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.DartFinancialResponse
import model.NaverNewsResponse
import service.DynamicNewsScraper
import service.SafeScraper
import service.UrlCacheManager

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

        install(ContentNegotiation) { json(Json { ignoreUnknownKeys = true })

        }
        install(Logging) {
            logger = Logger.DEFAULT
            level = LogLevel.NONE
        }
    }

    suspend fun fetchAndIngestNews(corpInfo: CorpInfo) {
        val clientId = "CqXQXHO3h0kqtYsXkePY" // 설정에서 가져오도록 수정 필요
        val clientSecret = "DODCxb1M4Z"
        var qlist = listOf<String>("${corpInfo.stockName} 분석","${corpInfo.stockName}[${corpInfo.stockCode}]", "${corpInfo.cName} 최근 동향", "${corpInfo.cName}")
        qlist.forEach { query ->
            try {
                val response: NaverNewsResponse = client.get("https://openapi.naver.com/v1/search/news.json") {
                    parameter("query", query)
                    parameter("display", 3) // 최근 10개 뉴스
                    parameter("sort", "sim") // 유사도 순 (또는 date 발간순)
                    header("X-Naver-Client-Id", clientId)
                    header("X-Naver-Client-Secret", clientSecret)
                }.body()
                    SafeScraper.scrapeParallel(corpInfo,response.items)
            } 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 "재무 데이터 없음"
                var buffer : StringBuffer = StringBuffer()
                buffer.append("[재무 분석 데이터]")
                response.list.forEach { it
                    buffer.append("${it.account_nm} (당기)${it?.thstrm_amount}, (전기)${it?.frmtrm_amount}").append("\n")
                }
                return buffer.toString()
            } catch (e: Exception) {
                "재무 API 연동 실패: ${e.message}"
            }
        } else {
            return ""
        }
    }


}