-- EasyCode front AI task center incremental upgrade

create table if not exists front_ai_generation_task (
  task_id              bigint(20)      not null auto_increment comment 'AI task ID',
  generation_id        bigint(20)      default null comment 'Generation record ID',
  project_id           bigint(20)      not null comment 'Project ID',
  user_id              bigint(20)      not null comment 'Front user ID',
  generate_type        varchar(50)     not null comment 'Generation type',
  provider             varchar(50)     default 'deepseek' comment 'AI provider',
  model                varchar(100)    default 'deepseek-chat' comment 'AI model',
  status               varchar(30)     not null default 'QUEUED' comment 'Task status',
  request_hash         varchar(64)     not null comment 'Request idempotency hash',
  request_payload      longtext        comment 'Request JSON',
  result_payload       longtext        comment 'Result JSON',
  error_code           varchar(100)    default '' comment 'Error code',
  error_message        varchar(1000)   default '' comment 'Error message',
  attempts             int             default 0 comment 'Attempt count',
  max_attempts         int             default 3 comment 'Max attempts',
  next_retry_time      datetime        default null comment 'Next retry time',
  locked_by            varchar(100)    default '' comment 'Worker lock owner',
  locked_until         datetime        default null comment 'Worker lock expiry',
  progress             int             default 0 comment 'Progress percent',
  current_step         varchar(100)    default '' comment 'Current step',
  input_tokens         int             default 0 comment 'Input tokens',
  output_tokens        int             default 0 comment 'Output tokens',
  total_tokens         int             default 0 comment 'Total tokens',
  cost_cents           int             default 0 comment 'Estimated cost in cents',
  started_at           datetime        default null comment 'Started time',
  finished_at          datetime        default null comment 'Finished time',
  create_time          datetime        default null comment 'Create time',
  update_time          datetime        default null comment 'Update time',
  primary key (task_id),
  key idx_front_ai_task_project (user_id, project_id),
  key idx_front_ai_task_status (status, next_retry_time),
  key idx_front_ai_task_generation (generation_id),
  key idx_front_ai_task_hash (user_id, project_id, request_hash)
) engine=innodb auto_increment=100 default charset=utf8mb4 comment='Front AI generation task';

create table if not exists front_ai_quota_bucket (
  bucket_id            bigint(20)      not null auto_increment comment 'Quota bucket ID',
  user_id              bigint(20)      not null comment 'Front user ID',
  period_type          varchar(20)     not null comment 'DAY or MONTH',
  period_key           varchar(20)     not null comment 'Period key',
  task_limit           int             default 0 comment 'Task limit',
  task_used            int             default 0 comment 'Used tasks',
  token_limit          int             default 0 comment 'Token limit',
  token_used           int             default 0 comment 'Used tokens',
  cost_limit_cents     int             default 0 comment 'Cost limit in cents',
  cost_used_cents      int             default 0 comment 'Used cost in cents',
  running_limit        int             default 1 comment 'Running task limit',
  running_count        int             default 0 comment 'Running task count',
  create_time          datetime        default null comment 'Create time',
  update_time          datetime        default null comment 'Update time',
  primary key (bucket_id),
  unique key uk_front_ai_quota_bucket (user_id, period_type, period_key)
) engine=innodb auto_increment=100 default charset=utf8mb4 comment='Front AI quota bucket';

create table if not exists front_ai_usage_ledger (
  ledger_id            bigint(20)      not null auto_increment comment 'Usage ledger ID',
  task_id              bigint(20)      default null comment 'Task ID',
  generation_id        bigint(20)      default null comment 'Generation record ID',
  project_id           bigint(20)      not null comment 'Project ID',
  user_id              bigint(20)      not null comment 'Front user ID',
  generate_type        varchar(50)     not null comment 'Generation type',
  provider             varchar(50)     default 'deepseek' comment 'AI provider',
  model                varchar(100)    default 'deepseek-chat' comment 'AI model',
  input_tokens         int             default 0 comment 'Input tokens',
  output_tokens        int             default 0 comment 'Output tokens',
  total_tokens         int             default 0 comment 'Total tokens',
  cost_cents           int             default 0 comment 'Estimated cost in cents',
  success              char(1)         default '0' comment 'Success flag',
  error_message        varchar(1000)   default '' comment 'Error message',
  create_time          datetime        default null comment 'Create time',
  primary key (ledger_id),
  key idx_front_ai_usage_user (user_id, create_time),
  key idx_front_ai_usage_task (task_id)
) engine=innodb auto_increment=100 default charset=utf8mb4 comment='Front AI usage ledger';
