import sys
import pytest
import os
from dotenv import load_dotenv

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))

SUCCESS_CODE = 200
VALIDATION_SUCCESSFUL_BUT_MANUAL_PUSH_FAILED = 504 # A 504 is returned when the validation is successful but the push to manual verification failed. This should only happen when running automated tests.

@pytest.fixture
def client():
    from app import app
    app.config['TESTING'] = True
    load_dotenv()
    return app.test_client()

# NOTE: Eventbrite discount code creation will not run if finalValidation is already true. Need to make a separate test file for testing integrations more directly.
# def test_verify_ugc_eventbrite_integration_success(client):
#     headers = {
#         'Authorization': os.environ.get("FLASK_API_KEY"),
#         'Content-Type': 'application/json'
#     }

#     # Prepare test data for a successful scenario
#     data = {
#         'ig_id': '8514169848', 
#         'required_tagged_username': '', 
#         'ig_is_private': 'false', 
#         'username': 'xplenture', 
#         'bot_username': 'null', 
#         'phone_number': '+12406888504',
#         'offer_id': 'C2Uk0WHF2OYfsCMw0cDk',
#         'client_id': 'q6fq5P1OkPbyZvC614a8', 
#         'location_id': '0', 
#         'content_type': 'ugc',
#         'ugc_url': 'https://firebasestorage.googleapis.com/v0/b/storyitapp-dev.appspot.com/o/Instagram-UGC%2Fq6fq5P1OkPbyZvC614a8%2F0%2F%2B12406888504_WriLOlidf3vUqvt0FQIM_q6fq5P1OkPbyZvC614a8_85_0_690212.webp?alt=media&token=9a8f87ce-87a4-49e4-be6d-011eff9a5ffe',
#         'specific_product': 'event', 
#         'client_name': 'Vuka'
#     }
    
    # response = client.post('/verify/verify-content/', json=data, headers=headers)
    # print(response.json)
    # assert response.status_code == SUCCESS_CODE or response.status_code == VALIDATION_SUCCESSFUL_BUT_MANUAL_PUSH_FAILED

def test_verify_ugc_success(client):
    headers = {
        'Authorization': os.environ.get("FLASK_API_KEY"),
        'Content-Type': 'application/json'
    }

    # Prepare test data for a successful scenario
    data = {
        'ig_id': '8514169848', 
        'required_tagged_username': '', 
        'ig_is_private': 'false', 
        'username': 'xplenture', 
        'bot_username': 'null', 
        'phone_number': '+12406888504',
        'offer_id': 'sJxyPXO8QhugO6yLFmEj',
        'client_id': 'CLKkSyvanqygdtgfBS2V', 
        'location_id': '0', 
        'content_type': 'ugc',
        'ugc_url': 'https://firebasestorage.googleapis.com/v0/b/storyitapp-dev.appspot.com/o/Temp-UGC%2F1F9514AF-22A1-4366-89D5-0F9064700CFF.JPG?alt=media',
        'specific_product': 'chain', 
        'client_name': 'Strapt'  
    }
    
    response = client.post('/verify/verify-content/', json=data, headers=headers)
    print(response.json)
    assert response.status_code == SUCCESS_CODE or response.status_code == VALIDATION_SUCCESSFUL_BUT_MANUAL_PUSH_FAILED


# NOTE: This endpoint call will only return 200 if the user does not have a story posted within the past 24 hours
def test_verify_story_success(client):
    headers = {
        'Authorization': os.environ.get("FLASK_API_KEY"),
        'Content-Type': 'application/json'
    }

    # Prepare test data for a successful scenario
    data = {
        'ig_id': '8514169848', 
        'required_tagged_username': 'straptofficial', 
        'ig_is_private': 'false', 
        'username': 'xplenture', 
        'bot_username': 'null',
        'phone_number': '+12406888504',
        'offer_id': 'C9m3pSPn2B27WZ4YTQOB',
        'client_id': 'CLKkSyvanqygdtgfBS2V', 
        'location_id': '0', 
        'content_type': 'instagramStory',
        'ugc_url': '', 
        'specific_product': '', 
        'client_name': 'Strapt'
    }
    
    response = client.post('/verify/verify-content/', json=data, headers=headers)
    assert response.status_code == SUCCESS_CODE or response.status_code == VALIDATION_SUCCESSFUL_BUT_MANUAL_PUSH_FAILED or response.status_code == 591 or response.status_code == 595


def test_verify_post_success(client):
    headers = {
        'Authorization': os.environ.get("FLASK_API_KEY"),
        'Content-Type': 'application/json'
    }

    data = {
        'ig_id': '8514169848', 
        'required_tagged_username': 'straptofficial', 
        'ig_is_private': 'false', 
        'username': 'xplenture', 
        'bot_username': 'null', 
        'phone_number': '+12406888504', 
        'offer_id': 'M2iP0zJUQCsi952MuZ44',
        'client_id': 'CLKkSyvanqygdtgfBS2V', 
        'location_id': '0', 
        'content_type': 'instagramPost',
        'ugc_url': '', 
        'specific_product': '', 
        'client_name': 'Strapt'
    }
    
    response = client.post('/verify/verify-content/', json=data, headers=headers)
    assert response.status_code == SUCCESS_CODE or response.status_code == VALIDATION_SUCCESSFUL_BUT_MANUAL_PUSH_FAILED


def test_no_matching_story_error_handling(client):
    # Define headers
    headers = {
        'Authorization': os.environ.get("FLASK_API_KEY"),
        'Content-Type': 'application/json'
    }

    # Try looking for a story that doesn't exist
    data = {
        'ig_id': '8514169848', 
        'required_tagged_username': 'adidas', 
        'ig_is_private': 'false', 
        'username': 'xplenture', 
        'bot_username': 'null',
        'phone_number': '+12406888504',
        'offer_id': 'C9m3pSPn2B27WZ4YTQOB',
        'client_id': 'CLKkSyvanqygdtgfBS2V',
        'location_id': '0', 
        'content_type': 'instagramStory',
        'ugc_url': '', 
        'specific_product': '', 
        'client_name': 'Strapt'
    }
    
    response = client.post('/verify/verify-content/', json=data, headers=headers)
    assert response.status_code == 591 or response.status_code == 595