<?php

defined('BASEPATH') or exit('No direct script access allowed');

add_option('propertybase_disable_properties_landing_page', '0');
add_option('propertybase_disable_properties_customer_side', '0');
add_option('propertybase_add_no_follow_tag_for_landingpage', '0');
add_option('propertybase_landingpage_seo_title', '');
add_option('propertybase_landingpage_seo_description', '');
add_option('propertybase_landingpage_seo_keywords', '');
add_option('propertybase_display_agent_profile', '1');
add_option('propertybase_google_analytics_code', '');
add_option('propertybase_hide_rented_and_sold_properties_from_listing', '1');
add_option('propertybase_translate_system_to_estate_terms', '0');
add_option('propertybase_active_theme', 'default');
add_option('propertybase_default_selected_status_for_leads', '');
add_option('propertybase_default_selected_source_for_leads', '');

if (!$CI->db->table_exists(db_prefix() . 'propertybase_properties')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_properties` (
  `id` int(11) NOT NULL,
  `name` text,
  `short_description` text,
  `long_description` text,
  `seo_keywords` text,
  `address` text,
  `country` int default 0, 
  `city` text,
  `zip_code` text,
  `size` text,
  `bathrooms` text,
  `bedrooms` text,
  `rooms` text,
  `floor` text,
  `energy_efficient` text,
  `gas_emission` text,
  `sale_price` text,
  `rent_price_incl` text,
  `rent_price_excl` text,
  `plan_image_path` text,
  `embeded_video` text,
  `ownership` int default 0, 
  `area` int default 0, 
  `energy_effiency` int default 0, 
  `category` int default 0, 
  `lat` text,
  `lng` text,
  `sections_to_hide_lp` text,
  `property_status` int default 1,
  `rel_id` text,
  `rel_type` text,
  `selected_agent` int default 0, 
  `total_views` int default 0,
  `property_purpose` int default 0,
  `total_leads` int default 0,
  `is_featured` int default 0, 
  `is_visible_for_public` int default 0, 
  `is_enabled` int default 0, 
  `created_at` datetime
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_properties`
  ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_properties`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_property_images')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_property_images` (
  `id` int(11) NOT NULL,
  `property_id` int(11),
  `image_url` text,
  `image_order` int default 0,
  `created_at` datetime
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_property_images`
  ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_property_images`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_properties_leads')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_properties_leads` (
  `id` int(11) NOT NULL,
  `property_id` int,
  `lead_id` int,
  `created_at` datetime
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_properties_leads`
  ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_properties_leads`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_categories')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_categories` (
  `id` int(11) NOT NULL,
  `name` text,
  `marker_icon` text,
  `is_enabled` int default 0,
  `created_at` datetime
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_categories`
  ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_categories`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_amenities')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_amenities` (
  `id` int(11) NOT NULL,
  `name` text,
  `icon` text,
  `category` int default 0, 
  `is_enabled` int default 0, 
  `created_at` datetime
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_amenities`
  ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_amenities`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_amenities_types')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_amenities_types` (
  `id` int(11) NOT NULL,
  `name` text,
  `is_enabled` int default 0, 
  `hide_on_landing_page` int default 0, 
  `created_at` datetime
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_amenities_types`
  ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_amenities_types`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_ownership_types')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_ownership_types` (
  `id` int(11) NOT NULL,
  `name` text,
  `is_enabled` int default 0,
  `created_at` datetime
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_ownership_types`
  ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_ownership_types`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_energy_efficiency_types')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_energy_efficiency_types` (
  `id` int(11) NOT NULL,
  `name` text,
  `is_enabled` int default 0,
  `created_at` datetime
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_energy_efficiency_types`
  ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_energy_efficiency_types`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_area_types')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_area_types` (
  `id` int(11) NOT NULL,
  `name` text,
  `value` text,
  `is_enabled` int default 0,
  `created_at` datetime
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_area_types`
  ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_area_types`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_properties_maintenance')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_properties_maintenance` (
  `id` int(11) NOT NULL,
  `property_id` int default 0,
  `maintenance_title` text,
  `maintenance_description` text,
  `user_id` int default 0,
  `requested_by` int default 0,
  `requested_by_type` text,
  `status` text,
  `created_at` datetime
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_properties_maintenance`
  ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_properties_maintenance`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_properties_audits')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_properties_audits` (
  `id` int(11) NOT NULL,
  `audit_name` text,
  `audit_by` int,
  `audit_date` text,
  `properties_list` text,
  `notes` text,
  `is_finished` int default 0, 
  `created_at` datetime
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_properties_audits`
  ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_properties_audits`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_property_amenities')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_property_amenities` (
      `id` int(11) NOT NULL,
      `property_id` int(11) NOT NULL,
      `amenity_id` int(11) NOT NULL,
      `created_at` datetime
    ) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_property_amenities`
      ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_property_amenities`
      MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_property_assigned')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_property_assigned` (
  `id` int(11) NOT NULL,
  `property_id` int default 0,
  `assigned_to` text,
  `assigned_id` text, 
  `assign_date` datetime
) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_property_assigned`
  ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_property_assigned`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_property_history')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_property_history` (
      `id` int(11) NOT NULL,
      `property_id` int(11) NOT NULL,
      `event` text,
      `field` text,
      `changed_from` text,
      `changed_to` text,
      `changed_by` int(11) NOT NULL,
      `changed_at` datetime
    ) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_property_history`
      ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_property_history`
      MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_property_events')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_property_events` (
      `id` int(11) NOT NULL,
      `property_id` int(11) NOT NULL,
      `event_by` int(11) DEFAULT 0,
      `event_form` text,
      `created_at` datetime
    ) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_property_events`
      ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_property_events`
      MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'propertybase_maintenance_request_files')) {
    $CI->db->query('CREATE TABLE `' . db_prefix() . "propertybase_maintenance_request_files` (
        `id` int(11) AUTO_INCREMENT,
        `request_id` int(11),
        `filename` varchar(255),
        `filepath` text,
        `created_at` datetime,
        PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_maintenance_request_files`
  ADD PRIMARY KEY (`id`);');

    $CI->db->query('ALTER TABLE `' . db_prefix() . 'propertybase_maintenance_request_files`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->field_exists('property_id', db_prefix() . 'invoices')) {
    $CI->db->query('ALTER TABLE `' . db_prefix() . 'invoices` ADD `property_id` INT DEFAULT 0;');
}

if (!$CI->db->field_exists('property_id', db_prefix() . 'expenses')) {
    $CI->db->query('ALTER TABLE `' . db_prefix() . 'expenses` ADD `property_id` INT DEFAULT 0;');
}

if (!$CI->db->field_exists('property_id', db_prefix() . 'contracts')) {
    $CI->db->query('ALTER TABLE `' . db_prefix() . 'contracts` ADD `property_id` INT DEFAULT 0;');
}