import pytest
import csv
from gpt_token_tracker.pricing import TOKENS_PER_PRICE


# -----------------------------
# Realtime CSV tests
# -----------------------------

def test_csv_writer_realtime(
    csv_token_logger_realtime,
    fake_realtime_usage,
    realtime_model,
    realtime_result,
    csv_writer_path,
):

    csv_token_logger_realtime.record(
        realtime_model,
        realtime_result,
        fake_realtime_usage,
    )

    with csv_writer_path.open() as f:
        header_row = f.readline()
        value_row = f.readline()

    assert header_row == "Model,Result,Total Tokens,Input Tokens,Output Tokens,Input Text Tokens,Input Audio Tokens,Input Image Tokens,Cached Text Tokens,Cached Audio Tokens,Cached Image Tokens,Output Text Tokens,Output Audio Tokens,Input Text Cost,Input Audio Cost,Input Image Cost,Cached Text Cost,Cached Audio Cost,Cached Image Cost,Text Output Cost,Audio Output Cost,Total Input Cost,Total Output Cost,Total Cost\n"
    assert value_row == 'gpt-realtime-mini,The capital of Ireland is Dublin,215,165,50,50,100,10,5,0,0,30,20,3e-05,0.001,5e-05,3e-07,0.0,0.0,7.2e-05,0.0004,0.0010803,0.00047200000000000003,0.0015523\n'


# -----------------------------
# Text realtime transcribe CSV tests
# -----------------------------


def test_csv_writer_transcribe_realtime(
    csv_token_logger_realtime_transcribe,
    fake_audio_transcription_usage,
    transcribe_model,
    realtime_transcription_result,
    csv_writer_path,
):

    csv_token_logger_realtime_transcribe.record(
        transcribe_model,
        realtime_transcription_result,
        fake_audio_transcription_usage,
    )

    with csv_writer_path.open() as f:
        header_row = f.readline()
        value_row = f.readline()

    assert header_row == 'Model,Result,Total Tokens,Input Tokens,Output Tokens,Input Text Tokens,Input Audio Tokens,Output Text Tokens,Input Text Cost,Input Audio Cost,Output Text Cost,Total Cost\n'
    assert value_row == 'gpt-4o-mini-transcribe,What is the capital of Ireland?,109,102,7,1,101,7,6e-07,0.00101,1.6800000000000002e-05,0.0010274\n'

# -----------------------------
# Text completion CSV tests
# -----------------------------

def test_csv_writer_text_completion(
    csv_token_logger_text_completion,
    fake_text_completion_usage,
    text_completion_model,
    text_completion_result,
    csv_writer_path,
):

    csv_token_logger_text_completion.record(
        text_completion_model,
        text_completion_result,
        fake_text_completion_usage,
    )

    with csv_writer_path.open() as f:
        header_row = f.readline()
        value_row = f.readline()

    assert header_row == "Model,Result,Total Tokens,Input Tokens,Output Tokens,Input Text Tokens,Cached Text Tokens,Output Text Tokens,Input Text Cost,Cached Text Cost,Output Text Cost,Total Cost\n"
    assert value_row == "gpt-5-mini,The capital of Ireland is Dublin,215,165,50,165,5,50,4.125e-05,1.25e-07,0.0001,0.000141375\n"


# -----------------------------
# Append behavior
# -----------------------------

def test_csv_writer_appends_rows(
    csv_token_logger_text_completion,
    fake_text_completion_usage,
    text_completion_model,
    text_completion_result,
    csv_writer_path,
):

    csv_token_logger_text_completion.record(
        text_completion_model,
        text_completion_result,
        fake_text_completion_usage,
    )
    csv_token_logger_text_completion.record(
        text_completion_model,
        text_completion_result,
        fake_text_completion_usage,
    )

    with csv_writer_path.open() as f:
        rows = list(csv.DictReader(f))

    assert len(rows) == 2
