// Workflow catalog: single source of truth built from each component's `meta`. See docs/implementation.md#workflow-catalog.
import FotoFirstBulkImportWorkflow from '@/components/workflow/workflows/FotoFirstBulkImportWorkflow.vue';
import BulkItemImportWorkflow from '@/components/workflow/workflows/BulkItemImportWorkflow.vue';
import InventoryAuditWorkflow from '@/components/workflow/workflows/InventoryAuditWorkflow.vue';
import StorageOptimizationWorkflow from '@/components/workflow/workflows/StorageOptimizationWorkflow.vue';
import MaintenanceScheduleWorkflow from '@/components/workflow/workflows/MaintenanceScheduleWorkflow.vue';
import ExpiryCheckWorkflow from '@/components/workflow/workflows/ExpiryCheckWorkflow.vue';
import BackupRestoreWorkflow from '@/components/workflow/workflows/BackupRestoreWorkflow.vue';

const workflows = [
    {...FotoFirstBulkImportWorkflow.meta, component: FotoFirstBulkImportWorkflow},
    {...BulkItemImportWorkflow.meta, component: BulkItemImportWorkflow},
    {...InventoryAuditWorkflow.meta, component: InventoryAuditWorkflow},
    {...StorageOptimizationWorkflow.meta, component: StorageOptimizationWorkflow},
    {...MaintenanceScheduleWorkflow.meta, component: MaintenanceScheduleWorkflow},
    {...ExpiryCheckWorkflow.meta, component: ExpiryCheckWorkflow},
    {...BackupRestoreWorkflow.meta, component: BackupRestoreWorkflow},
];

export function getAllWorkflows() {
    return workflows;
}

export function getWorkflow(slug) {
    return workflows.find(workflow => workflow.slug === slug);
}

export function getWorkflowComponent(id) {
    return getWorkflow(id)?.component || null;
}

export function getWorkflowsByCategory(category) {
    return workflows.filter(workflow => workflow.category === category);
}

export function getWorkflowCategories() {
    return [...new Set(workflows.map(workflow => workflow.category))];
}

// Builds the payload sent to the backend to start a new instance of this workflow.
export function buildWorkflowApiPayload(workflow) {
    const ownPayload = workflow.getInitialPayload ? workflow.getInitialPayload() : {};
    return {
        title: workflow.title,
        slug: workflow.slug,
        state: 'running',
        current_step: 1,
        total_steps: workflow.stepDefinitions.length,
        payload: {
            ...ownPayload
        }
    };
}

// Payload is an opaque string to the backend; frontend serializes/deserializes it. See docs/implementation.md#workflow-payload-is-an-opaque-string.
export function serializeWorkflowPayload(workflow) {
    console.log(workflow);
    if (!workflow || !('payload' in workflow)) return workflow;
    return {...workflow, payload: JSON.stringify(workflow.payload ?? {})};
}

export function deserializeWorkflowPayload(workflow) {
    if (!workflow) return workflow;
    let payload = {};
    try {
        payload = workflow.payload ? JSON.parse(workflow.payload) : {};
    } catch (e) {
        console.error('Failed to parse workflow payload JSON:', e);
    }
    return {...workflow, payload};
}

export default {
    getAllWorkflows,
    getWorkflow,
    getWorkflowComponent,
    getWorkflowsByCategory,
    getWorkflowCategories,
    buildWorkflowApiPayload,
    serializeWorkflowPayload,
    deserializeWorkflowPayload
};
