create table if not exists dashboard_analytics_panel_config (
  id bigint generated by default as identity primary key,
  tenant_id bigint not null,
  enabled boolean not null default false,
  title varchar(100) not null default '经营分析',
  subtitle varchar(255),
  empty_state_text varchar(255),
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

create table if not exists dashboard_analytics_card_config (
  id bigint generated by default as identity primary key,
  tenant_id bigint not null,
  card_key varchar(100) not null,
  group_name varchar(100),
  title varchar(100) not null,
  subtitle varchar(255),
  render_type varchar(20) not null default 'metric',
  sql_template text not null,
  value_field varchar(100) not null default 'value',
  description_field varchar(100),
  category_field varchar(100),
  color_field varchar(100),
  display_text_config text,
  unit varchar(20),
  value_type varchar(20) not null default 'number',
  sort_direction varchar(20) not null default 'sql',
  display_limit integer,
  layout_type varchar(20) not null default 'vertical',
  full_row boolean not null default false,
  link_path varchar(255),
  sort_order integer not null default 0,
  enabled boolean not null default true,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now()
);

alter table dashboard_analytics_panel_config
  add column if not exists enabled boolean not null default false;

alter table dashboard_analytics_panel_config
  add column if not exists title varchar(100) not null default '经营分析';

alter table dashboard_analytics_panel_config
  add column if not exists subtitle varchar(255);

alter table dashboard_analytics_panel_config
  add column if not exists empty_state_text varchar(255);

alter table dashboard_analytics_card_config
  add column if not exists group_name varchar(100);

alter table dashboard_analytics_card_config
  add column if not exists title varchar(100) not null default '未命名卡片';

alter table dashboard_analytics_card_config
  add column if not exists subtitle varchar(255);

alter table dashboard_analytics_card_config
  add column if not exists render_type varchar(20) not null default 'metric';

alter table dashboard_analytics_card_config
  add column if not exists description_field varchar(100);

alter table dashboard_analytics_card_config
  add column if not exists category_field varchar(100);

alter table dashboard_analytics_card_config
  add column if not exists color_field varchar(100);

alter table dashboard_analytics_card_config
  add column if not exists display_text_config text;

alter table dashboard_analytics_card_config
  add column if not exists unit varchar(20);

alter table dashboard_analytics_card_config
  add column if not exists value_type varchar(20) not null default 'number';

alter table dashboard_analytics_card_config
  add column if not exists sort_direction varchar(20) not null default 'sql';

alter table dashboard_analytics_card_config
  add column if not exists display_limit integer;

alter table dashboard_analytics_card_config
  add column if not exists layout_type varchar(20) not null default 'vertical';

alter table dashboard_analytics_card_config
  add column if not exists full_row boolean not null default false;

alter table dashboard_analytics_card_config
  add column if not exists link_path varchar(255);

do $$
begin
  if not exists (
    select 1
    from pg_constraint
    where conname = 'uk_dashboard_analytics_panel_tenant'
  ) then
    alter table dashboard_analytics_panel_config
      add constraint uk_dashboard_analytics_panel_tenant unique (tenant_id);
  end if;
end
$$;

do $$
begin
  if not exists (
    select 1
    from pg_constraint
    where conname = 'uk_dashboard_analytics_card_tenant_key'
  ) then
    alter table dashboard_analytics_card_config
      add constraint uk_dashboard_analytics_card_tenant_key unique (tenant_id, card_key);
  end if;
end
$$;

create index if not exists idx_dashboard_analytics_card_tenant_sort
  on dashboard_analytics_card_config (tenant_id, sort_order asc, id asc);
