alter table crm_opportunity
  add column if not exists channel_expansion_id bigint;

create index if not exists idx_crm_opportunity_channel_expansion
  on crm_opportunity(channel_expansion_id);

do $$
begin
  if not exists (
    select 1
    from pg_constraint
    where conname = 'fk_crm_opportunity_channel_expansion'
  ) then
    alter table crm_opportunity
      add constraint fk_crm_opportunity_channel_expansion
      foreign key (channel_expansion_id) references crm_channel_expansion(id);
  end if;
end $$;

alter table crm_channel_expansion
  add column if not exists channel_code varchar(50),
  add column if not exists office_address varchar(255),
  add column if not exists channel_industry varchar(100),
  add column if not exists contact_established_date date,
  add column if not exists intent_level varchar(20),
  add column if not exists has_desktop_exp boolean,
  add column if not exists channel_attribute varchar(100),
  add column if not exists internal_attribute varchar(100);

update crm_channel_expansion
set channel_industry = coalesce(channel_industry, industry)
where channel_industry is null and industry is not null;

update crm_channel_expansion
set intent_level = coalesce(intent_level, 'medium'),
    has_desktop_exp = coalesce(has_desktop_exp, false);

alter table crm_channel_expansion
  alter column intent_level set default 'medium',
  alter column intent_level set not null,
  alter column has_desktop_exp set default false,
  alter column has_desktop_exp set not null;

do $$
begin
  if not exists (
    select 1
    from pg_constraint
    where conname = 'crm_channel_expansion_intent_level_check'
  ) then
    alter table crm_channel_expansion
      add constraint crm_channel_expansion_intent_level_check
      check (intent_level in ('high', 'medium', 'low'));
  end if;
end $$;

create table if not exists crm_channel_expansion_contact (
  id bigint generated by default as identity primary key,
  channel_expansion_id bigint not null,
  contact_name varchar(50),
  contact_mobile varchar(20),
  contact_title varchar(100),
  sort_order integer not null default 1,
  created_at timestamptz not null default now(),
  updated_at timestamptz not null default now(),
  constraint fk_crm_channel_expansion_contact_channel
    foreign key (channel_expansion_id) references crm_channel_expansion(id) on delete cascade
);

create index if not exists idx_crm_channel_expansion_contact_channel
  on crm_channel_expansion_contact(channel_expansion_id);

insert into crm_channel_expansion_contact (channel_expansion_id, contact_name, contact_mobile, contact_title, sort_order, created_at, updated_at)
select c.id, c.contact_name, c.contact_mobile, c.contact_title, 1, now(), now()
from crm_channel_expansion c
where (coalesce(btrim(c.contact_name), '') <> '' or coalesce(btrim(c.contact_mobile), '') <> '' or coalesce(btrim(c.contact_title), '') <> '')
  and not exists (
    select 1
    from crm_channel_expansion_contact cc
    where cc.channel_expansion_id = c.id
  );

alter table crm_expansion_followup
  add column if not exists visit_start_time timestamptz,
  add column if not exists evaluation_content text,
  add column if not exists next_plan text;
