create table if not exists agent_task
(
    id           bigserial
        primary key,
    project_uuid uuid                                                          not null,
    parent_id    bigint
        constraint fk_agent_task_parent
            references agent_task
            on delete set null,
    agent_type   varchar(20)              default 'react'::character varying   not null,
    status       varchar(20)              default 'pending'::character varying not null,
    title        varchar(255),
    input        text                                                          not null,
    output       text,
    error        text,
    created_by   uuid,
    created_at   timestamp with time zone default now()                        not null,
    updated_at   timestamp with time zone default now()                        not null,
    started_at   timestamp with time zone,
    done_at      timestamp with time zone,
    progress     varchar(255),
    issue_id     uuid,
    retry_count  integer                  default 0
);

create index if not exists idx_agent_task_project
    on agent_task (project_uuid);

create index if not exists idx_agent_task_parent
    on agent_task (parent_id);

create index if not exists idx_agent_task_status
    on agent_task (status);

create index if not exists idx_agent_task_created_by
    on agent_task (created_by);

create index if not exists idx_agent_task_created_at
    on agent_task (created_at);

create index if not exists idx_agent_task_issue
    on agent_task (issue_id);

create index if not exists idx_agent_task_retry_count
    on agent_task (retry_count);
