//! Common types for zsqlite
//!
//! Contains enums, flags, and type definitions shared across modules.

const c = @import("c.zig").c;

/// Flags for opening a database connection
pub const OpenFlags = struct {
    read_only: bool = false,
    read_write: bool = true,
    create: bool = true,
    uri: bool = false,
    memory: bool = false,
    no_mutex: bool = false,
    full_mutex: bool = false,

    /// Converts flags to SQLite integer format
    pub fn toInt(self: OpenFlags) c_int {
        var flags: c_int = 0;

        if (self.read_only) {
            flags |= c.SQLITE_OPEN_READONLY;
        } else if (self.read_write) {
            flags |= c.SQLITE_OPEN_READWRITE;
            if (self.create) {
                flags |= c.SQLITE_OPEN_CREATE;
            }
        }

        if (self.uri) flags |= c.SQLITE_OPEN_URI;
        if (self.memory) flags |= c.SQLITE_OPEN_MEMORY;
        if (self.no_mutex) flags |= c.SQLITE_OPEN_NOMUTEX;
        if (self.full_mutex) flags |= c.SQLITE_OPEN_FULLMUTEX;

        return flags;
    }
};

/// SQLite column types
pub const ColumnType = enum(c_int) {
    integer = 1,
    float = 2,
    text = 3,
    blob = 4,
    null_value = 5,

    pub fn fromInt(value: c_int) ColumnType {
        return switch (value) {
            1 => .integer,
            2 => .float,
            3 => .text,
            4 => .blob,
            else => .null_value,
        };
    }
};

/// SQLite limit types for getLimit/setLimit
pub const Limit = enum(c_int) {
    /// Maximum length of a string or BLOB
    length = 0,
    /// Maximum length of a SQL statement
    sql_length = 1,
    /// Maximum number of columns
    column = 2,
    /// Maximum depth of expression tree
    expr_depth = 3,
    /// Maximum number of terms in compound SELECT
    compound_select = 4,
    /// Maximum number of VDBE operations
    vdbe_op = 5,
    /// Maximum number of function arguments
    function_arg = 6,
    /// Maximum number of attached databases
    attached = 7,
    /// Maximum length of LIKE/GLOB pattern
    like_pattern_length = 8,
    /// Maximum number of bound parameters
    variable_number = 9,
    /// Maximum depth of trigger recursion
    trigger_depth = 10,
    /// Maximum number of worker threads
    worker_threads = 11,
};

/// Operation type for update hooks
pub const UpdateOperation = enum(c_int) {
    insert = 18, // SQLITE_INSERT
    update = 23, // SQLITE_UPDATE
    delete = 9, // SQLITE_DELETE

    pub fn fromInt(value: c_int) ?UpdateOperation {
        return switch (value) {
            18 => .insert,
            23 => .update,
            9 => .delete,
            else => null,
        };
    }
};

/// Authorization action codes
pub const AuthAction = enum(c_int) {
    create_index = 1,
    create_table = 2,
    create_temp_index = 3,
    create_temp_table = 4,
    create_temp_trigger = 5,
    create_temp_view = 6,
    create_trigger = 7,
    create_view = 8,
    delete = 9,
    drop_index = 10,
    drop_table = 11,
    drop_temp_index = 12,
    drop_temp_table = 13,
    drop_temp_trigger = 14,
    drop_temp_view = 15,
    drop_trigger = 16,
    drop_view = 17,
    insert = 18,
    pragma = 19,
    read = 20,
    select = 21,
    transaction = 22,
    update = 23,
    attach = 24,
    detach = 25,
    alter_table = 26,
    reindex = 27,
    analyze = 28,
    create_vtable = 29,
    drop_vtable = 30,
    function = 31,
    savepoint = 32,
    recursive = 33,

    pub fn fromInt(value: c_int) ?AuthAction {
        if (value >= 1 and value <= 33) {
            return @enumFromInt(value);
        }
        return null;
    }
};

/// Authorization result codes
pub const AuthResult = enum(c_int) {
    /// Allow the action
    ok = 0,
    /// Deny the action with an error
    deny = 1,
    /// Silently disallow (return NULL or skip)
    ignore = 2,
};
