CREATE TABLE `employee_catalog` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `employee_id` BIGINT UNSIGNED NOT NULL,
  `partner_id` BIGINT UNSIGNED NOT NULL,
  `catalog_id` BIGINT UNSIGNED NOT NULL,
  PRIMARY KEY (`id`))

CREATE TABLE partner_url (
  agency_id INT NOT NULL,
  partner_url VARCHAR(64) NOT NULL,
  updated_at DATETIME NOT NULL,
  PRIMARY KEY (agency_id),
  UNIQUE KEY uq_partner_url (partner_url)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `partners`
  ADD COLUMN `phone` VARCHAR(50) NULL AFTER `name`,
  ADD COLUMN `email` VARCHAR(191) NULL AFTER `phone`;

ALTER TABLE `partners_files`
  ADD COLUMN `partner_id` BIGINT UNSIGNED NULL AFTER `employee_id`,
  ADD INDEX `partner_files_partner_id_index` (`partner_id`);

CREATE TABLE partner_props (
  id INT NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (id)
) ENGINE=InnoDB;


ALTER TABLE partner_props
  ADD agency_id INT NOT NULL,
  ADD partner_id INT NOT NULL;


ALTER TABLE partner_props
  ADD UNIQUE KEY uq_partner_props_agency_partner (agency_id, partner_id),
  ADD KEY idx_partner_props_agency (agency_id),
  ADD KEY idx_partner_props_partner (partner_id);


ALTER TABLE partner_props
  ADD legal_name VARCHAR(255) NOT NULL DEFAULT '',
  ADD actual_address VARCHAR(500) NOT NULL DEFAULT '',
  ADD legal_address VARCHAR(500) NOT NULL DEFAULT '';


ALTER TABLE partner_props
  ADD inn VARCHAR(12) NOT NULL DEFAULT '',
  ADD ogrn VARCHAR(13) NOT NULL DEFAULT '',
  ADD kpp VARCHAR(9) NOT NULL DEFAULT '';

ALTER TABLE partner_props
  ADD bank_name VARCHAR(255) NOT NULL DEFAULT '',
  ADD bik VARCHAR(9) NOT NULL DEFAULT '',
  ADD corr_account VARCHAR(20) NOT NULL DEFAULT '',
  ADD settlement_account VARCHAR(20) NOT NULL DEFAULT '';


ALTER TABLE partner_props
  ADD ceo_full_name VARCHAR(255) NOT NULL DEFAULT '';


ALTER TABLE partner_props
  CONVERT TO CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci;


CREATE TABLE partner_avatars (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  employee_id INT UNSIGNED NOT NULL,
  partner_id INT UNSIGNED NOT NULL,
  avatar_logo VARCHAR(255) NOT NULL,
  path VARCHAR(255) NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_partner_avatars_employee (employee_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


CREATE TABLE partner_material_categories (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  agency_id INT UNSIGNED NOT NULL,
  title VARCHAR(190) NOT NULL,
  PRIMARY KEY (id),
  KEY idx_agency (agency_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE partner_material_documents (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  agency_id INT UNSIGNED NOT NULL,
  category_id INT UNSIGNED NOT NULL,
  title VARCHAR(190) NOT NULL,
  doc_type ENUM('file','link','text') NOT NULL DEFAULT 'file',
  file_path VARCHAR(255) NOT NULL DEFAULT '',
  file_name VARCHAR(255) NOT NULL DEFAULT '',
  file_mime VARCHAR(120) NOT NULL DEFAULT '',
  file_size INT UNSIGNED NOT NULL DEFAULT 0,
  link_url VARCHAR(500) NOT NULL DEFAULT '',
  text_content MEDIUMTEXT,
  PRIMARY KEY (id),
  KEY idx_category (category_id),
  KEY idx_agency (agency_id),
  CONSTRAINT fk_pmd_category
    FOREIGN KEY (category_id) REFERENCES partner_material_categories(id)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;



CREATE TABLE IF NOT EXISTS partner_news (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  agency_id INT UNSIGNED NOT NULL,
  title VARCHAR(255) NOT NULL,
  body_html MEDIUMTEXT
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_unicode_ci
    NOT NULL,
  is_important TINYINT(1) NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL,
  updated_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  KEY idx_agency_id (agency_id),
  KEY idx_created_at (created_at)
) ENGINE=InnoDB
DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci;

ALTER TABLE partner_news
  MODIFY `title` MEDIUMTEXT
  CHARACTER SET utf8mb4
  COLLATE utf8mb4_unicode_ci
  NOT NULL;