# pxy_de/pipelines/pxy_products/run_products.py
from django.db import transaction
from pxy_de.models import PipelineRun, PipelineStep, PipelineProductRun
from .fetch_data import fetch_data
from .create_category import create_categories
from .create_user_menus import create_user_menus

def record_pipeline_step(pipeline_run, step_name, status):
    # Retrieve the PipelineStep object. Create it if it doesn't exist
    step, _ = PipelineStep.objects.get_or_create(name=step_name)

    # Create a record for this pipeline step
    PipelineProductRun.objects.create(
        pipeline_run=pipeline_run,
        step=step,
        status=status,
    )

def run_pipeline():
    # Start a new pipeline run
    current_run = PipelineRun.objects.create()
    step_log = ""

    try:
        # Fetch data from API
        data = fetch_data()
        if data:
            step_log = 'Fetch Data'
            record_pipeline_step(current_run, step_log , 'Success')

            # Create categories from the fetched data
            step_log = 'Create Categories'
            create_categories(data)
            record_pipeline_step(current_run, step_log, 'Success')

            # Create user menus for each category
            step_log = 'Create User Menus'
            create_user_menus(data)
            record_pipeline_step(current_run, step_log, 'Success')

            # Mark the overall pipeline run as successful
            current_run.overall_status = 'Success'
        else:
            # If fetching data fails, record it and set the overall run status to 'Failed'
            record_pipeline_step(current_run, 'Fetch Data', 'Failed')
            current_run.overall_status = 'Failed'

    except Exception as e:
        # If any exception occurs, record it and set the overall run status to 'Failed'
        record_pipeline_step(current_run, f'Error in {step_log}' ,str(e))
        current_run.overall_status = 'Failed'

    finally:
        # Save any changes to the current run (like the overall status)
        current_run.save()