import CodexBarCore
import Foundation
import Testing
@testable import CodexBar

struct ClaudeAdminAPIInlineDashboardModelTests {
    @Test
    func `claude admin api usage gets inline dashboard`() throws {
        let now = try Self.localNoon(year: 2023, month: 11, day: 17)
        let bucketDay = try Self.localNoon(year: 2023, month: 11, day: 14)
        let metadata = try #require(ProviderDefaults.metadata[.claude])
        let usage = ClaudeAdminAPIUsageSnapshot(
            daily: [
                ClaudeAdminAPIUsageSnapshot.DailyBucket(
                    day: "2023-11-14",
                    startTime: bucketDay,
                    endTime: bucketDay.addingTimeInterval(86400),
                    costUSD: 1.25,
                    inputTokens: 1000,
                    cacheCreationInputTokens: 400,
                    cacheReadInputTokens: 300,
                    outputTokens: 250,
                    totalTokens: 1950,
                    costItems: [
                        ClaudeAdminAPIUsageSnapshot.CostBreakdown(name: "Claude Sonnet Usage", costUSD: 1.25),
                    ],
                    models: [
                        ClaudeAdminAPIUsageSnapshot.ModelBreakdown(
                            name: "claude-sonnet-4-20250514",
                            inputTokens: 1000,
                            cacheCreationInputTokens: 400,
                            cacheReadInputTokens: 300,
                            outputTokens: 250,
                            totalTokens: 1950),
                    ]),
            ],
            updatedAt: now)

        let model = UsageMenuCardView.Model.make(.init(
            provider: .claude,
            metadata: metadata,
            snapshot: usage.toUsageSnapshot(),
            credits: nil,
            creditsError: nil,
            dashboard: nil,
            dashboardError: nil,
            tokenSnapshot: nil,
            tokenError: nil,
            account: AccountInfo(email: nil, plan: nil),
            isRefreshing: false,
            lastError: nil,
            usageBarsShowUsed: false,
            resetTimeDisplayStyle: .countdown,
            tokenCostUsageEnabled: false,
            showOptionalCreditsAndExtraUsage: true,
            hidePersonalInfo: false,
            now: now))

        #expect(model.metrics.isEmpty)
        #expect(model.inlineUsageDashboard == nil)
        #expect(model.providerDetails.first?.rows.first?.value == "$0.00")
        #expect(model.providerDetails.first?.chart?.points.first?.label == "2023-11-14")
        #expect(model.providerDetails.first?.chart?.points.first?.value == 1.25)
        #expect(model.providerDetails.first?.rows.contains { $0.label == "30d tokens" } == true)
        #expect(model.providerDetails.first?.rows.contains {
            $0.label == "Top model" && $0.value == "claude-sonnet-4-20250514"
        } == true)
        #expect(model.planText == "Admin API")
    }

    private static func localNoon(year: Int, month: Int, day: Int) throws -> Date {
        try #require(Calendar.current.date(from: DateComponents(year: year, month: month, day: day, hour: 12)))
    }
}
