#pragma once

#include <efi.h>

// IDT entry (64-bit gate descriptor)
struct idt_entry {
    UINT16 offset_low;
    UINT16 selector;
    UINT8  ist;          // IST index (bits 0-2), reserved (bits 3-6), type (bit 7)
    UINT8  type_attr;    // P (bit 7), DPL (bits 5-6), 0 (bit 4), type (bits 0-3)
    UINT16 offset_mid;
    UINT32 offset_high;
    UINT32 reserved;
} __attribute__((packed));

struct idt_ptr {
    UINT16 limit;
    UINT64 base;
} __attribute__((packed));

// Interrupt frame saved by ISR stubs
struct trap_frame {
    // Pushed by ISR stub (general-purpose registers)
    UINT64 r11;
    UINT64 r10;
    UINT64 r9;
    UINT64 r8;
    UINT64 rdi;
    UINT64 rsi;
    UINT64 rdx;
    UINT64 rcx;
    UINT64 rax;
    // Pushed by ISR stub (metadata)
    UINT64 vector;
    UINT64 error_code;
    // Pushed by CPU (interrupt frame)
    UINT64 rip;
    UINT64 cs;
    UINT64 rflags;
    UINT64 rsp;
    UINT64 ss;
} __attribute__((packed));

// Handler function type
typedef void (*isr_handler_t)(trap_frame*);

void idt_init(void);
void idt_set_handler(UINT8 vector, isr_handler_t handler);
