/*
 * main.c - CLI entry point for hiperiso-log.
 *
 * Usage:
 *   hiperiso-log analyze <log_dir>
 *   hiperiso-log trace <trace.bin> [--format json|csv|text]
 *   hiperiso-log serial <serial.log> [--stages]
 */

#include "trace_parser.h"
#include "serial_parser.h"
#include "report.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void usage(const char *prog)
{
    fprintf(stderr, "Usage: %s analyze <log_dir>\n", prog);
    fprintf(stderr, "       %s trace <trace.bin> [--format json|csv|text]\n", prog);
    fprintf(stderr, "       %s serial <serial.log> [--stages]\n", prog);
}

static int cmd_analyze(const char *log_dir)
{
    if (report_generate(log_dir) != 0) {
        fprintf(stderr, "Error: failed to generate report for '%s'\n", log_dir);
        return 1;
    }

    printf("Report generated successfully in %s/\n", log_dir);
    printf("  report.json - machine-readable analysis\n");
    printf("  report.txt  - human-readable summary\n");
    return 0;
}

static int cmd_trace(const char *trace_file, const char *format)
{
    trace_summary_t summary;
    if (trace_parse_file(trace_file, &summary) != 0) {
        fprintf(stderr, "Error: failed to parse trace file '%s'\n", trace_file);
        return 1;
    }

    if (strcmp(format, "json") == 0)
        trace_print_json(&summary, stdout);
    else if (strcmp(format, "csv") == 0)
        trace_print_csv(&summary, stdout);
    else
        trace_print_text(&summary, stdout);

    trace_free_summary(&summary);
    return 0;
}

static int cmd_serial(const char *serial_file, int show_stages)
{
    serial_summary_t summary;
    if (serial_parse_file(serial_file, &summary) != 0) {
        fprintf(stderr, "Error: failed to parse serial log '%s'\n", serial_file);
        return 1;
    }

    if (show_stages)
        serial_print_stages(&summary, stdout);
    else
        serial_print_text(&summary, stdout);

    serial_free_summary(&summary);
    return 0;
}

int main(int argc, char **argv)
{
    if (argc < 2) {
        usage(argv[0]);
        return 1;
    }

    if (strcmp(argv[1], "analyze") == 0) {
        if (argc < 3) {
            usage(argv[0]);
            return 1;
        }
        return cmd_analyze(argv[2]);
    }

    if (strcmp(argv[1], "trace") == 0) {
        if (argc < 3) {
            usage(argv[0]);
            return 1;
        }
        const char *format = "text";
        for (int i = 3; i < argc; i++) {
            if (strcmp(argv[i], "--format") == 0 && i + 1 < argc) {
                format = argv[i + 1];
                i++;
            }
        }
        return cmd_trace(argv[2], format);
    }

    if (strcmp(argv[1], "serial") == 0) {
        if (argc < 3) {
            usage(argv[0]);
            return 1;
        }
        int show_stages = 0;
        for (int i = 3; i < argc; i++) {
            if (strcmp(argv[i], "--stages") == 0)
                show_stages = 1;
        }
        return cmd_serial(argv[2], show_stages);
    }

    usage(argv[0]);
    return 1;
}
