START TRANSACTION;
ALTER TABLE `tblclients` ADD `user_type` ENUM('client','lead') NOT NULL DEFAULT 'client' AFTER `userid`;
ALTER TABLE `tblclients` ADD `from_lead` TINYINT(1) DEFAULT 0 AFTER `user_type`;
ALTER TABLE `tblclients` ADD `added_on` DATETIME NULL AFTER `datecreated`;
ALTER TABLE `tblclients` ADD `updated_on` DATETIME NULL AFTER `added_on`;
ALTER TABLE `tblclients` ADD `added_by` BIGINT NULL AFTER `addedfrom`;
ALTER TABLE `tblclients` ADD `updated_by` BIGINT NULL AFTER `added_by`;
UPDATE `tblclients` SET added_on = datecreated, updated_on = datecreated, added_by = addedfrom, updated_by = addedfrom;

CREATE TABLE `tbllead_details` (
    `id` BIGINT UNSIGNED NOT NULL , 
    `tenant_id` BIGINT UNSIGNED NOT NULL , 
    `client_id` BIGINT UNSIGNED NOT NULL , 
    `description` TEXT NULL , 
    `number` BIGINT NULL , 
    `prefix` VARCHAR(50) NULL ,
    `number_format` BIGINT NULL , 
    `lead_number_text` VARCHAR(191) NULL , 
    `leadorder` bigint(20) UNSIGNED DEFAULT '1',
    `title` varchar(100) DEFAULT NULL,
    `hash` VARCHAR(65) NULL , 
    `status` BIGINT UNSIGNED NOT NULL , 
    `source` BIGINT UNSIGNED NOT NULL , 
    `lastcontact` DATETIME NULL , 
    `assigned` BIGINT UNSIGNED DEFAULT 0,
    `dateassigned` DATE NULL , 
    `last_status_change` DATETIME NULL , 
    `date_converted` DATETIME NULL , 
    `is_public` TINYINT(1) DEFAULT 0,
    `is_deleted` TINYINT(1) DEFAULT 0
    ) ENGINE = InnoDB;


--
-- Indexes for table `tbllead_details`
--
ALTER TABLE `tbllead_details`
  ADD PRIMARY KEY (`id`),
  ADD KEY `tenant_id` (`tenant_id`),
  ADD KEY `client_id` (`client_id`),
  ADD KEY `assigned` (`assigned`),
  ADD KEY `status` (`status`),
  ADD KEY `source` (`source`),
  ADD KEY `lastcontact` (`lastcontact`),
  ADD KEY `leadorder` (`leadorder`),
  ADD KEY `is_deleted` (`is_deleted`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tbllead_details`
--
ALTER TABLE `tbllead_details`
    MODIFY `id` bigint UNSIGNED NOT NULL AUTO_INCREMENT;

ALTER TABLE tbllead_details
    ADD CONSTRAINT fk_lead_detail_tenant_id
    FOREIGN KEY (tenant_id)
    REFERENCES tbltenants(id)
    ON DELETE CASCADE
    ON UPDATE CASCADE;

ALTER TABLE `tbl_leads_conversations` ADD `client_id` BIGINT UNSIGNED NULL AFTER `lead_id`;
ALTER TABLE `tbl_leads_conversations` CHANGE `lead_id` `lead_id` BIGINT UNSIGNED NULL;

ALTER TABLE `tbllead_activity_log` ADD `client_id` BIGINT UNSIGNED NULL AFTER `leadid`;
ALTER TABLE `tbllead_activity_log` CHANGE `leadid` `leadid` BIGINT UNSIGNED NULL;

ALTER TABLE `tblleads` ADD `mapped_client_id` BIGINT UNSIGNED NULL;

COMMIT;