import g4f
from g4f import Provider
import json

# Updated system prompt for JSON format podcast generation
SYSTEM_PROMPT = '''
You are a podcast script generator. Create a natural dialogue between two hosts discussing the given topic.

CRITICAL: You must respond ONLY with valid JSON in the exact format specified below. Do not include any text outside the JSON structure.

MOST IMPORTANT : EVERY ASPECT OF JSON STRUCTURE SHOULD BE PROPERLY FOLLOWED
Required JSON format:
{
    "title": "Podcast Title",
    "voice_assignments": {
        "SPEAKER1": "nova",
        "SPEAKER2": "onyx"
    },
    "segments": [
        {

            "speaker": "SPEAKER1",
            "text": "Hello world"
        },
       {

            "speaker": "SPEAKER1",
            "text": "Hello world"

        }
    ]
}

Content Guidelines:
1. Create 30-40 exchanges between SPEAKER1 and SPEAKER2
2. Use educational, conversational tone
3. Include appropriate sound effects and pauses
4. Emotions can be: happy, excited, curious, thoughtful, surprised, neutral

Podcast Style Instructions:
1. Opening: Begin with welcoming phrase "Hey everyone, welcome back"
2. Dialog Structure: Conversational back-and-forth with affirmations like "Right," "Exactly," "Absolutely"
3. Language: Informal, accessible, enthusiastic
4. Content: Introduce sources early, use analogies, break down complex ideas
5. Host Interaction: One asks, other explains, validate each other
6. Audience Engagement: Address listeners directly
7. Structure: Start broad, narrow down, summarize key points
8. Closing: "So as we wrap things up..." and "Until next time, keep exploring"

Segment Types:
- "speech": Dialogue with speaker, text, and emotion
- "sfx": Sound effects with description and duration
- "pause": Silent pauses with duration in seconds

Make it engaging, informative, and educational while maintaining the conversational podcast format.

REMEMBER: Output ONLY valid JSON. No markdown, no explanations, just the JSON structure.
'''

# Podcast discussion generation function
def generate_podcast_discussion(topic_prompt: str):

    client = g4f.Client(provider=Provider.Blackbox)

        # Optional image context (can be removed if not required)
    images = [
        [open("1.jpg", "rb"), "1.jpeg"],
        [open("2.jpg", "rb"), "2.webp"]
    ]

    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": topic_prompt}
    ]

        # Type ignore comment to silence the type checker error
    response = client.chat.completions.create(
        model="gpt-5-nano",
        provider="PollinationsAI",
        messages=messages, images=images)  # type: ignore
    print(response)

        # Parse and validate JSON response
       
    # podcast_data = json.loads(response.choices[0].message.content)
    # print(json.dumps(podcast_data, indent=2))
     

# Example usage
if __name__ == "__main__":
    topic =SYSTEM_PROMPT + "Create a conversational podcast  format between two people with more than 30 dialogue discussing the images given in an educational tone in requested json structure."
    generate_podcast_discussion(topic)