--- client-details

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

  first_name     VARCHAR2(1024 CHAR),
  middle_name    VARCHAR2(1024 CHAR),
  last_name      VARCHAR2(1024 CHAR),

  sex            VARCHAR2(255 CHAR),
  birthday       VARCHAR2(255 CHAR),
  cellphone      VARCHAR2(255 CHAR),
  email          VARCHAR2(255 CHAR),

  ext_ref        VARCHAR2(1024 CHAR),
  card_ref       VARCHAR2(1024 CHAR),

  branch_id      VARCHAR2(1024 CHAR),
  branch_name    VARCHAR2(1024 CHAR),
  branch_ext_ref VARCHAR2(1024 CHAR),
  branch_bik     VARCHAR2(1024 CHAR),

  created_at     TIMESTAMP WITH TIME ZONE NOT NULL,
  updated_at     TIMESTAMP WITH TIME ZONE 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
);
