--- client-details

CREATE TABLE client_details (
  client_id      BIGINT PRIMARY KEY REFERENCES clients (id),

  first_name     NVARCHAR(1024),
  middle_name    NVARCHAR(1024),
  last_name      NVARCHAR(1024),

  sex            NVARCHAR(255),
  birthday       NVARCHAR(255),
  cellphone      NVARCHAR(255),
  email          NVARCHAR(255),

  ext_ref        NVARCHAR(1024),
  card_ref       NVARCHAR(1024),

  branch_id      NVARCHAR(1024),
  branch_name    NVARCHAR(1024),
  branch_ext_ref NVARCHAR(1024),
  branch_bik     NVARCHAR(1024),

  created_at     DATETIMEOFFSET NOT NULL,
  updated_at     DATETIMEOFFSET NOT NULL
);

-- Migrate existing clients.

INSERT INTO client_details (
  client_id,
  first_name,
  middle_name,
  last_name,

  sex,
  birthday,
  cellphone,
  email,

  ext_ref,
  card_ref,

  branch_id,
  branch_name,
  branch_ext_ref,
  branch_bik,

  created_at,
  updated_at
) (
  SELECT
    clients.id,

    c.first_name,
    c.middle_name,
    c.last_name,

    c.sex,
    c.birthday,
    c.cellphone,
    c.email,

    c.ext_ref,
    c.card_ref,

    c.branch_id,
    c.branch_name,
    c.branch_ext_ref,
    c.branch_bik,

    c.created_at,
    c.updated_at
  FROM clients
    JOIN crm_customers c ON c.id = clients.customer_id
);
