/**
 * @package   akeebabackup
 * @copyright Copyright 2006-2026 Nicholas K. Dionysopoulos / Akeeba Ltd
 * @license   GNU General Public License version 3, or later
 */

--
-- Create the Profiles table
--
CREATE TABLE IF NOT EXISTS "#__akeebabackup_profiles" (
    id integer GENERATED BY DEFAULT AS IDENTITY,
    description varchar(255) NOT NULL,
    configuration text,
    filters text,
    quickicon smallint NOT NULL DEFAULT 1,
    access integer DEFAULT 1,
    PRIMARY KEY (id)
);

--
-- Create the default backup profile
--
INSERT INTO "#__akeebabackup_profiles"
    (id, description, configuration, filters, quickicon)
VALUES (1, 'Default Backup Profile', '', '', 1)
ON CONFLICT (id) DO NOTHING;

--
-- Create the backups table
--
CREATE TABLE IF NOT EXISTS "#__akeebabackup_backups" (
    id bigint GENERATED BY DEFAULT AS IDENTITY,
    description varchar(255) NOT NULL,
    comment text,
    backupstart timestamp without time zone DEFAULT NULL,
    backupend timestamp without time zone DEFAULT NULL,
    status varchar(8) NOT NULL DEFAULT 'run',
    origin varchar(30) NOT NULL DEFAULT 'backend',
    type varchar(30) NOT NULL DEFAULT 'full',
    profile_id bigint NOT NULL DEFAULT 1,
    archivename text,
    absolute_path text,
    multipart integer NOT NULL DEFAULT 0,
    tag varchar(255) DEFAULT NULL,
    backupid varchar(255) DEFAULT NULL,
    filesexist smallint NOT NULL DEFAULT 1,
    remote_filename varchar(1000) DEFAULT NULL,
    total_size bigint NOT NULL DEFAULT 0,
    frozen smallint NOT NULL DEFAULT 0,
    instep smallint NOT NULL DEFAULT 0,
    PRIMARY KEY (id),
    CONSTRAINT "akeebabackup_backups_status_check"
        CHECK (status IN ('run', 'fail', 'complete'))
);

CREATE INDEX IF NOT EXISTS "idx_akeebabackup_backups_fullstatus"
    ON "#__akeebabackup_backups" (filesexist, status);
CREATE INDEX IF NOT EXISTS "idx_akeebabackup_backups_stale"
    ON "#__akeebabackup_backups" (status, origin);

--
-- Create the custom storage table
--
CREATE TABLE IF NOT EXISTS "#__akeebabackup_storage" (
    tag varchar(255) NOT NULL,
    lastupdate timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    data text,
    PRIMARY KEY (tag)
);

--
-- Create the common table for all Akeeba extensions.
--
-- This table is never uninstalled when uninstalling the extensions themselves.
--
CREATE TABLE IF NOT EXISTS "#__akeeba_common" (
    key varchar(190) NOT NULL,
    value text NOT NULL,
    PRIMARY KEY (key)
);
