import requests

class LucidicAPI:
    def __init__(self, api_key: str):
        self.api_key = api_key
        self.headers = {"Authorization": f"Api-Key {self.api_key}"}
        self.base_url = "https://dashboard.lucidic.ai/demo/api/v1"
        self.endpoints = {
            "verifyAPIKey": "verifyAPIkey",
            "initializejob": "initializejob",
            "assumeAWSS3Role": "getS3creds",
        }
    def _makeRequest(self, endpoint, params=None):
        try:
            url = f'{self.base_url}/{self.endpoints[endpoint]}'
            response = requests.get(
                url,
                headers=self.headers,
                params=params
            )
            response.raise_for_status()
            return response
        except requests.exceptions.RequestException as e:
            print(f"Error during API Call, status {response.status_code}: {e}")
            raise
        except KeyError as e:
            print(f"Error: Specified endpoint not found.")
            raise

    def verifyAPIKey(self):
        response = self._makeRequest('verifyAPIKey')
        return response.json().get('project', None)
        
    def startJob(self, pathToDataFolder):
        print(f"Starting job, verifying API key...")
        project = self.verifyAPIKey()
        assert project is not None
        print(f"API Key verified for project {project}!")
        print(f"Initializing job...")
        jobInitializationResponseJSON = self._makeRequest('initializejob').json()
        print(f"{jobInitializationResponseJSON}")
        jobID = jobInitializationResponseJSON.get('jobID', None)
        assert jobID is not None
        print(f"Job Initialized with jobID: {jobID}")
        print(f"Issuing temporary AWS S3 Credentials...")
        credsResponseJSON = self._makeRequest('assumeAWSS3Role').json()
        AccessKeyId = credsResponseJSON.get('AccessKeyId', None)
        SecretAccessKey = credsResponseJSON.get('SecretAccessKey', None)
        SessionToken = credsResponseJSON.get('SessionToken', None)
        Expiration = credsResponseJSON.get('Expiration', None)
        print(f"Temporary AWS Credentials Issued: {AccessKeyId}, {SecretAccessKey}, {SessionToken}, {Expiration}")





