-- CreateTable
CREATE TABLE `api_log` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `user_id` INTEGER NOT NULL,
    `created_at` DATETIME(0) NOT NULL,
    `ip` VARCHAR(15) NOT NULL,
    `url` VARCHAR(255) NOT NULL,
    `method` VARCHAR(15) NOT NULL,
    `data` TEXT NULL,
    `status` VARCHAR(255) NOT NULL,

    INDEX `ix_api_log_created_at`(`created_at`),
    INDEX `ix_api_log_user_id`(`user_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `branches` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(80) NOT NULL,
    `active` TINYINT NOT NULL DEFAULT 1,
    `visible` TINYINT NOT NULL DEFAULT 1,
    `state_id` INTEGER NOT NULL,
    `folder` VARCHAR(60) NOT NULL,
    `latitude` DOUBLE NULL,
    `longitude` DOUBLE NULL,
    `map_description` TEXT NULL,

    UNIQUE INDEX `ix_branches_title`(`title`),
    UNIQUE INDEX `ix_branches_folder`(`folder`),
    INDEX `fk_branches_states_link`(`state_id`),
    INDEX `ix_branches_active`(`active`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `categories` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(80) NOT NULL,
    `sort_order` INTEGER UNSIGNED NOT NULL DEFAULT 10,
    `hashtags` VARCHAR(255) NOT NULL DEFAULT '',
    `old_id` INTEGER NULL,
    `status` ENUM('active', 'blocked') NOT NULL DEFAULT 'active',
    `owner_id` INTEGER NULL,
    `emails` VARCHAR(512) NOT NULL DEFAULT '{}',
    `type` ENUM('public', 'private', 'curated', 'association') NOT NULL DEFAULT 'public',
    `discount` INTEGER NOT NULL DEFAULT 0,

    INDEX `ix_categories_sort_order`(`sort_order`),
    INDEX `ix_categories_status`(`status`),
    INDEX `ix_categories_title`(`title`),
    INDEX `ix_categories_type`(`type`),
    INDEX `ix_cats_owner_id`(`owner_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `category_communities` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `category_id` INTEGER NOT NULL,
    `community_id` INTEGER NOT NULL,

    INDEX `fk_cc_community_id`(`community_id`),
    UNIQUE INDEX `ix_cc_main`(`category_id`, `community_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `category_members` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `category_id` INTEGER NOT NULL,
    `user_id` INTEGER NULL,
    `email` VARCHAR(150) NOT NULL,
    `status` ENUM('pending', 'pre-approved', 'curated', 'declined', 'blocked') NOT NULL DEFAULT 'pending',
    `created_at` DATETIME(0) NULL,

    INDEX `fk_category_members_user_id`(`user_id`),
    INDEX `ix_category_members_email`(`email`),
    UNIQUE INDEX `ix_category_members_main`(`category_id`, `email`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `communities` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `branch_id` INTEGER NULL,
    `title` VARCHAR(80) NOT NULL,
    `sort_order` TINYINT NOT NULL DEFAULT 1,
    `old_id` INTEGER NULL,
    `status` ENUM('active', 'blocked') NOT NULL DEFAULT 'active',
    `timezone` VARCHAR(32) NOT NULL DEFAULT 'America/Los_Angeles',

    INDEX `ix_communities_branch_id`(`branch_id`),
    INDEX `ix_communities_sort_order`(`sort_order`),
    INDEX `ix_communities_status`(`status`),
    INDEX `ix_communities_title`(`title`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `countries` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(64) NOT NULL,
    `short_title` VARCHAR(3) NOT NULL,

    UNIQUE INDEX `ix_countries_title`(`title`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `event_categories` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `event_id` INTEGER NOT NULL,
    `category_id` INTEGER NOT NULL,
    `status` ENUM('on_review', 'approved', 'declined') NOT NULL DEFAULT 'on_review',

    INDEX `fk_evcat_cat`(`category_id`),
    INDEX `ix_ec_status`(`status`),
    UNIQUE INDEX `ix_evcat_main`(`event_id`, `category_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `event_shares` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `event_id` INTEGER NOT NULL,
    `soc_account_id` INTEGER NOT NULL,
    `service_item_id` VARCHAR(255) NOT NULL,

    INDEX `ix_event_shares_event_id`(`event_id`),
    INDEX `ix_event_shares_service_item_id`(`service_item_id`),
    INDEX `ix_event_shares_soc_account_id`(`soc_account_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `event_socials` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `event_id` INTEGER NOT NULL,
    `soc_account_id` INTEGER NOT NULL,

    INDEX `ix_event_socials_event_id`(`event_id`),
    INDEX `ix_event_socials_soc_account_id`(`soc_account_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `event_times` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `event_id` INTEGER NOT NULL,
    `start_at` DATETIME(0) NOT NULL,
    `end_at` DATETIME(0) NOT NULL,
    `tweet_sent` DATETIME(0) NULL,
    `url_code` VARCHAR(8) NOT NULL DEFAULT '',

    INDEX `ix_event_times_tweet_sent`(`tweet_sent`),
    INDEX `ix_event_times_url_code`(`url_code`),
    UNIQUE INDEX `ix_evtimes_main`(`event_id`, `start_at`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `events` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `owner_id` INTEGER NOT NULL,
    `title` VARCHAR(255) NOT NULL,
    `start_at` DATETIME(0) NOT NULL,
    `end_at` DATETIME(0) NOT NULL,
    `description` TEXT NOT NULL,
    `image` VARCHAR(255) NOT NULL DEFAULT '',
    `url` VARCHAR(255) NOT NULL DEFAULT '',
    `is_all_ages` TINYINT NOT NULL DEFAULT 0,
    `is_free` TINYINT NOT NULL DEFAULT 0,
    `status` ENUM('draft', 'on_review', 'approved', 'declined', 'unverified', 'website_only') NOT NULL DEFAULT 'draft',
    `rec_frequency` ENUM('none', 'daily', 'weekly', 'monthly', 'annually', 'sessions') NOT NULL DEFAULT 'none',
    `rec_interval` TINYINT NOT NULL DEFAULT 1,
    `rec_days_of_week` VARCHAR(32) NOT NULL DEFAULT '{}',
    `rec_repeat_by` ENUM('day_of_month', 'day_of_week') NOT NULL DEFAULT 'day_of_month',
    `rec_end_rule` ENUM('never', 'on', 'after') NOT NULL DEFAULT 'never',
    `rec_repeats` INTEGER NOT NULL DEFAULT 1,
    `rec_end_on` DATE NULL,
    `old_id` INTEGER NULL,
    `is_calculated` TINYINT NOT NULL DEFAULT 0,
    `is_21` TINYINT NOT NULL DEFAULT 0,
    `color` VARCHAR(32) NOT NULL DEFAULT '',
    `tweet_text` VARCHAR(140) NOT NULL DEFAULT '',
    `sessions` TEXT NOT NULL,
    `facebook_url` VARCHAR(255) NOT NULL DEFAULT '',
    `venue_id` INTEGER NULL,
    `tweet_before` ENUM('now', '1min', '1hour', '1day', '2days', '1week') NOT NULL DEFAULT '1day',
    `created_at` DATETIME(0) NOT NULL DEFAULT ('2000-01-01 00:00:00'),
    `updated_at` DATETIME(0) NULL,
    `notification_sent` DATETIME(0) NULL,
    `is_series` TINYINT NOT NULL DEFAULT 0,
    `more_info_url` VARCHAR(255) NULL,
    `recur_notify_sent` DATETIME(0) NULL,
    `is_for_website_only` TINYINT NOT NULL DEFAULT 0,
    `online_event_link` VARCHAR(255) NULL,

    INDEX `ix_evcat_status`(`status`),
    INDEX `ix_events_owner`(`owner_id`),
    INDEX `ix_events_start_at`(`start_at`),
    INDEX `ix_events_status`(`status`),
    INDEX `ix_events_venue_id`(`venue_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `fields` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `field_type` ENUM('text', 'textarea', 'checkbox', 'checkbox_group', 'radio_group', 'select', 'yes_no', 'address', 'working_hours') NOT NULL DEFAULT 'text',
    `default` VARCHAR(255) NULL,
    `options` TEXT NULL,
    `title` TEXT NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `galleries` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(64) NOT NULL,
    `description` VARCHAR(255) NOT NULL DEFAULT '',
    `options` TEXT NOT NULL,

    UNIQUE INDEX `ix_galleries_title`(`title`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `gallery_images` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `gallery_id` INTEGER NOT NULL,
    `sort_order` INTEGER NOT NULL DEFAULT 0,
    `title` VARCHAR(64) NOT NULL,
    `description` VARCHAR(255) NOT NULL DEFAULT '',
    `url` VARCHAR(2100) NOT NULL DEFAULT '',
    `filename_base` VARCHAR(255) NOT NULL,
    `ext` VARCHAR(10) NOT NULL,

    INDEX `ix_gallery_images_gallery_id`(`gallery_id`),
    INDEX `ix_gallery_images_sort_order`(`sort_order`),
    INDEX `ix_gallery_images_title`(`title`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `global_contents` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `branch_id` INTEGER NOT NULL,
    `title` VARCHAR(64) NOT NULL,
    `description` VARCHAR(255) NOT NULL DEFAULT '',
    `content` TEXT NOT NULL,
    `wysiwyg` TINYINT NOT NULL DEFAULT 1,

    INDEX `ix_gcb_branch_id`(`branch_id`),
    UNIQUE INDEX `ix_gcb_main`(`branch_id`, `title`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `listing_category` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `listing_id` INTEGER NOT NULL,
    `category_id` INTEGER NOT NULL,

    INDEX `fk_cc_listing_category_id_cat`(`category_id`),
    UNIQUE INDEX `ix_cc_lc_main`(`listing_id`, `category_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `listing_field` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `listing_id` INTEGER NOT NULL,
    `subcategory_id` INTEGER NOT NULL,
    `field_id` INTEGER NOT NULL,
    `value` TEXT NULL,

    UNIQUE INDEX `ix_fields_listing_subcat`(`field_id`, `listing_id`, `subcategory_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `listing_user` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `listing_id` INTEGER NOT NULL,
    `user_id` INTEGER NOT NULL,

    UNIQUE INDEX `ix_cc_lu_main`(`listing_id`, `user_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `listings` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `alias` VARCHAR(255) NOT NULL,
    `title` VARCHAR(255) NOT NULL,
    `description` TEXT NOT NULL,
    `open_hours` TEXT NULL,
    `address` VARCHAR(255) NULL DEFAULT '',
    `phone` VARCHAR(255) NOT NULL,
    `email` VARCHAR(255) NOT NULL,
    `website` VARCHAR(255) NULL,
    `fb_page` VARCHAR(255) NULL,
    `insta_page` VARCHAR(255) NULL,
    `logo` VARCHAR(255) NULL,
    `venue_id` INTEGER NULL,
    `json` LONGTEXT NULL,
    `status` ENUM('draft', 'on_review', 'claimed', 'approved', 'declined') NOT NULL DEFAULT 'draft',
    `level` ENUM('level1', 'level2', 'level3') NOT NULL DEFAULT 'level1',
    `area_id` INTEGER NULL,
    `owner` INTEGER NULL,

    UNIQUE INDEX `alias`(`alias`),
    INDEX `fk_cc_listing_area_id`(`area_id`),
    INDEX `ix_listings_alias`(`alias`),
    INDEX `ix_listings_id`(`id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `migration` (
    `version` VARCHAR(180) NOT NULL,
    `apply_time` INTEGER NULL,

    PRIMARY KEY (`version`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `news` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(250) NOT NULL,
    `published_at` DATE NOT NULL,
    `url` VARCHAR(255) NOT NULL DEFAULT '',

    INDEX `ix_news_published_at`(`published_at`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `pages` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `branch_id` INTEGER NOT NULL,
    `title` VARCHAR(255) NOT NULL,
    `content` TEXT NOT NULL,
    `alias` VARCHAR(255) NOT NULL,
    `sort_order` TINYINT NOT NULL DEFAULT 0,
    `visible` TINYINT NOT NULL DEFAULT 1,
    `params` VARCHAR(4000) NOT NULL DEFAULT '{}',
    `parent_id` INTEGER NULL,
    `is_home` TINYINT NOT NULL DEFAULT 0,
    `type` VARCHAR(60) NOT NULL DEFAULT '',
    `menu_title` VARCHAR(255) NOT NULL,
    `menu_visible` TINYINT NOT NULL DEFAULT 1,
    `keywords` VARCHAR(255) NOT NULL DEFAULT '',
    `description` VARCHAR(255) NOT NULL DEFAULT '',
    `template` VARCHAR(255) NOT NULL DEFAULT 'default',
    `for_users` TINYINT NOT NULL DEFAULT 1,
    `for_guests` TINYINT NOT NULL DEFAULT 1,

    INDEX `ix_pages_branch`(`branch_id`),
    INDEX `ix_pages_parent_id`(`parent_id`),
    INDEX `ix_pages_sort`(`sort_order`),
    INDEX `ix_pages_visible`(`visible`),
    UNIQUE INDEX `ix_pages_alias`(`branch_id`, `alias`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `social_accounts` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `owner_id` INTEGER NOT NULL,
    `owner_type` ENUM('user', 'branch', 'community') NOT NULL DEFAULT 'branch',
    `service` VARCHAR(80) NOT NULL,
    `account_id` VARCHAR(255) NOT NULL,
    `user_token` VARCHAR(1024) NOT NULL DEFAULT '',
    `user_secret` VARCHAR(1024) NOT NULL DEFAULT '',
    `account` VARCHAR(128) NOT NULL DEFAULT '',

    INDEX `ix_social_accounts_owner_id`(`owner_id`),
    INDEX `ix_social_accounts_owner_type`(`owner_type`),
    INDEX `ix_social_accounts_service`(`service`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `states` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(64) NOT NULL,
    `short_title` CHAR(2) NOT NULL DEFAULT '',
    `country_id` INTEGER NOT NULL,

    UNIQUE INDEX `ix_states_title`(`title`),
    INDEX `fk_states_countries_link`(`country_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `subcategories` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `alias` VARCHAR(255) NULL,
    `title` VARCHAR(255) NULL,
    `image` VARCHAR(255) NULL,
    `descr` TEXT NULL,
    `fields` VARCHAR(255) NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `templates_email` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `alias` VARCHAR(255) NOT NULL,
    `subject` VARCHAR(255) NOT NULL,
    `from` VARCHAR(255) NULL,
    `to` VARCHAR(255) NULL,
    `bcc` VARCHAR(255) NULL,
    `html` TEXT NOT NULL,
    `descr` TEXT NULL,
    `css` TEXT NULL,

    UNIQUE INDEX `alias`(`alias`),
    INDEX `ix_templates_email_alias`(`alias`),
    INDEX `ix_templates_email_id`(`id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `ticket_attendees` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `event_id` INTEGER NOT NULL,
    `ticketype_id` INTEGER NOT NULL,
    `title` VARCHAR(255) NOT NULL,
    `price` DOUBLE NOT NULL,
    `first_name` VARCHAR(255) NOT NULL,
    `last_name` VARCHAR(255) NOT NULL,
    `email` VARCHAR(255) NOT NULL,
    `checked` BOOLEAN NULL DEFAULT false,
    `invoice` VARCHAR(255) NOT NULL,
    `issue_date` DATETIME(0) NULL,
    `check_date` DATETIME(0) NULL,
    `code` VARCHAR(255) NOT NULL,

    INDEX `fk-ticket_attendees-tycketype_id`(`ticketype_id`),
    INDEX `idx-ticket_attendees-event_id-ticketype_id`(`event_id`, `ticketype_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `ticketypes` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(255) NOT NULL,
    `seats` INTEGER NOT NULL,
    `price` DOUBLE NOT NULL,
    `opened` DATETIME(0) NULL,
    `closed` DATETIME(0) NULL,
    `event_id` INTEGER NOT NULL,

    INDEX `idx-ticketypes-event_id`(`event_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `topcategories` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `order` INTEGER NULL,
    `area_id` INTEGER NULL,
    `alias` VARCHAR(255) NULL,
    `title` VARCHAR(255) NULL,
    `image` VARCHAR(255) NULL,
    `subs` TEXT NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `user_logs` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `created_at` DATETIME(0) NOT NULL,
    `user_id` INTEGER NOT NULL,
    `action` VARCHAR(256) NOT NULL,
    `params` VARCHAR(1024) NOT NULL DEFAULT '{}',
    `note` VARCHAR(255) NOT NULL DEFAULT '',
    `creator_id` INTEGER NULL,

    INDEX `ix_ul_creator_id`(`creator_id`),
    INDEX `ix_ulogs_created_at`(`created_at`),
    INDEX `ix_ulogs_user_id`(`user_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `user_news` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `user_id` INTEGER NOT NULL,
    `news_id` INTEGER NOT NULL,
    `is_viewed` TINYINT NOT NULL DEFAULT 0,

    INDEX `ix_user_news_news_id`(`news_id`),
    INDEX `ix_user_news_user_id`(`user_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `user_user` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `owner_id` INTEGER NOT NULL,
    `manager_id` INTEGER NOT NULL,

    INDEX `fk_cc_user_manager_id`(`manager_id`),
    UNIQUE INDEX `ix_cc_uu_main`(`owner_id`, `manager_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `users` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `email` VARCHAR(80) NOT NULL,
    `password` VARCHAR(80) NOT NULL,
    `created_at` DATETIME(0) NOT NULL,
    `status` ENUM('registered', 'active', 'blocked') NOT NULL DEFAULT 'registered',
    `expires` DATE NOT NULL DEFAULT ('2020-12-31'),
    `is_trial` TINYINT NOT NULL DEFAULT 0,
    `stripe_token` VARCHAR(32) NOT NULL DEFAULT '--LOCAL_FREE_CUSTOMER--',
    `first_name` VARCHAR(80) NOT NULL DEFAULT '',
    `last_name` VARCHAR(80) NOT NULL DEFAULT '',
    `address` VARCHAR(80) NOT NULL DEFAULT '',
    `address2` VARCHAR(80) NOT NULL DEFAULT '',
    `city` VARCHAR(80) NOT NULL DEFAULT '',
    `state` VARCHAR(80) NOT NULL DEFAULT '',
    `zip` VARCHAR(10) NOT NULL DEFAULT '',
    `organization` VARCHAR(80) NOT NULL DEFAULT '',
    `phone` VARCHAR(80) NOT NULL DEFAULT '',
    `options` VARCHAR(2048) NOT NULL DEFAULT '{}',
    `auth_key` VARCHAR(32) NOT NULL DEFAULT '',
    `password_reset_token` VARCHAR(255) NOT NULL DEFAULT '',
    `old_id` INTEGER NULL,
    `role` ENUM('developer', 'admin', 'curator', 'ent', 'pro', 'free') NOT NULL DEFAULT 'free',
    `url` VARCHAR(255) NOT NULL DEFAULT '',
    `google_calendar` VARCHAR(255) NOT NULL DEFAULT '',
    `invited_by` INTEGER NULL,
    `discount` VARCHAR(10) NOT NULL DEFAULT '0',
    `api_app_token` CHAR(16) NULL,
    `api_auth_token` CHAR(16) NULL,
    `reward` INTEGER NOT NULL DEFAULT 0,

    UNIQUE INDEX `ix_users_email`(`email`),
    INDEX `ix_users_api_app_token`(`api_app_token`),
    INDEX `ix_users_api_auth_token`(`api_auth_token`),
    INDEX `ix_users_auth_key`(`auth_key`),
    INDEX `ix_users_expires`(`expires`),
    INDEX `ix_users_status`(`status`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `venue_categories` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `alias` VARCHAR(255) NOT NULL,
    `title` VARCHAR(255) NOT NULL,
    `sort_order` INTEGER NOT NULL,
    `image` VARCHAR(255) NULL,
    `descr` TEXT NULL,
    `area_id` INTEGER NULL,

    UNIQUE INDEX `alias`(`alias`),
    INDEX `fk_area_id`(`area_id`),
    INDEX `ix_venue_categories_alias`(`alias`),
    INDEX `ix_venue_categories_id`(`id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `venuecategory_venue` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `venuecategory_id` INTEGER NOT NULL,
    `venue_id` INTEGER NOT NULL,

    INDEX `fk_cc_venue_id`(`venue_id`),
    UNIQUE INDEX `ix_cc_main`(`venuecategory_id`, `venue_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `venues` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(200) NOT NULL,
    `zip` VARCHAR(16) NOT NULL DEFAULT '',
    `city` VARCHAR(255) NULL,
    `address` VARCHAR(100) NOT NULL DEFAULT '',
    `full_address` VARCHAR(200) NOT NULL DEFAULT '',
    `community_id` INTEGER NOT NULL DEFAULT 1,
    `creator_id` INTEGER NULL,
    `latitude` DOUBLE NULL,
    `longitude` DOUBLE NULL,

    INDEX `fk_venues_creator_id`(`creator_id`),
    INDEX `ix_venues_community_id`(`community_id`),
    INDEX `ix_venues_full_address`(`full_address`),
    INDEX `ix_venues_title`(`title`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `widgets` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `owner_id` INTEGER NOT NULL,
    `title` VARCHAR(80) NOT NULL,
    `active` TINYINT NOT NULL DEFAULT 1,
    `params` TEXT NOT NULL,
    `___DEL_branch_id` INTEGER NULL,
    `type` ENUM('list', 'calendar', 'cards') NOT NULL DEFAULT 'list',

    INDEX `ix_widgets_branch_id`(`___DEL_branch_id`),
    INDEX `ix_widgets_owner`(`owner_id`),
    UNIQUE INDEX `ix_widgets_title_owner`(`owner_id`, `title`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `widgets_duda` (
    `duda_id` VARCHAR(43) NOT NULL,
    `mcs_id` VARCHAR(20) NULL,

    PRIMARY KEY (`duda_id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `branches` ADD CONSTRAINT `fk_branches_states_link` FOREIGN KEY (`state_id`) REFERENCES `states`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `categories` ADD CONSTRAINT `fk_cats_owner_id` FOREIGN KEY (`owner_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `category_communities` ADD CONSTRAINT `fk_cc_category_id` FOREIGN KEY (`category_id`) REFERENCES `categories`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `category_communities` ADD CONSTRAINT `fk_cc_community_id` FOREIGN KEY (`community_id`) REFERENCES `communities`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `category_members` ADD CONSTRAINT `fk_category_members_category_id` FOREIGN KEY (`category_id`) REFERENCES `categories`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `category_members` ADD CONSTRAINT `fk_category_members_user_id` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `communities` ADD CONSTRAINT `fk_coms_branch` FOREIGN KEY (`branch_id`) REFERENCES `branches`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `event_categories` ADD CONSTRAINT `fk_evcat_cat` FOREIGN KEY (`category_id`) REFERENCES `categories`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `event_categories` ADD CONSTRAINT `fk_evcat_event` FOREIGN KEY (`event_id`) REFERENCES `events`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `event_shares` ADD CONSTRAINT `fk_event_shares_event` FOREIGN KEY (`event_id`) REFERENCES `events`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `event_shares` ADD CONSTRAINT `fk_event_shares_soc_account_id` FOREIGN KEY (`soc_account_id`) REFERENCES `social_accounts`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `event_socials` ADD CONSTRAINT `fk_event_socials_event` FOREIGN KEY (`event_id`) REFERENCES `events`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `event_socials` ADD CONSTRAINT `fk_event_socials_soc_account_id` FOREIGN KEY (`soc_account_id`) REFERENCES `social_accounts`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `event_times` ADD CONSTRAINT `fk_evtimes_event` FOREIGN KEY (`event_id`) REFERENCES `events`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `events` ADD CONSTRAINT `fk_events_owner` FOREIGN KEY (`owner_id`) REFERENCES `users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `events` ADD CONSTRAINT `fk_events_venue_id` FOREIGN KEY (`venue_id`) REFERENCES `venues`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `gallery_images` ADD CONSTRAINT `fk_gallery_images_gallery_id` FOREIGN KEY (`gallery_id`) REFERENCES `galleries`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `global_contents` ADD CONSTRAINT `fk_gcb_branch_id` FOREIGN KEY (`branch_id`) REFERENCES `branches`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `listing_category` ADD CONSTRAINT `fk_cc_listing_category_id` FOREIGN KEY (`listing_id`) REFERENCES `listings`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `listing_category` ADD CONSTRAINT `fk_cc_listing_category_id_cat` FOREIGN KEY (`category_id`) REFERENCES `subcategories`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `listings` ADD CONSTRAINT `fk_cc_listing_area_id` FOREIGN KEY (`area_id`) REFERENCES `branches`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `pages` ADD CONSTRAINT `fk_pages_branch_id` FOREIGN KEY (`branch_id`) REFERENCES `branches`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `pages` ADD CONSTRAINT `fk_pages_parent_id` FOREIGN KEY (`parent_id`) REFERENCES `pages`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `states` ADD CONSTRAINT `fk_states_countries_link` FOREIGN KEY (`country_id`) REFERENCES `countries`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `ticket_attendees` ADD CONSTRAINT `fk-ticket_attendees-event_id` FOREIGN KEY (`event_id`) REFERENCES `events`(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;

-- AddForeignKey
ALTER TABLE `ticket_attendees` ADD CONSTRAINT `fk-ticket_attendees-tycketype_id` FOREIGN KEY (`ticketype_id`) REFERENCES `ticketypes`(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;

-- AddForeignKey
ALTER TABLE `ticketypes` ADD CONSTRAINT `fk-ticketypes-event_id` FOREIGN KEY (`event_id`) REFERENCES `events`(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;

-- AddForeignKey
ALTER TABLE `user_logs` ADD CONSTRAINT `fk_ul_creator_id` FOREIGN KEY (`creator_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `user_logs` ADD CONSTRAINT `fk_ulogs_user` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `user_news` ADD CONSTRAINT `fk_user_news_news_id` FOREIGN KEY (`news_id`) REFERENCES `news`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `user_news` ADD CONSTRAINT `fk_user_news_user_id` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `user_user` ADD CONSTRAINT `fk_cc_owner_id` FOREIGN KEY (`owner_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `user_user` ADD CONSTRAINT `fk_cc_user_manager_id` FOREIGN KEY (`manager_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `venue_categories` ADD CONSTRAINT `fk_area_id` FOREIGN KEY (`area_id`) REFERENCES `branches`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `venuecategory_venue` ADD CONSTRAINT `fk_cc_venue_id` FOREIGN KEY (`venue_id`) REFERENCES `venues`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `venuecategory_venue` ADD CONSTRAINT `fk_cc_venuecategory_id` FOREIGN KEY (`venuecategory_id`) REFERENCES `venue_categories`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `venues` ADD CONSTRAINT `fk_venues_community_id` FOREIGN KEY (`community_id`) REFERENCES `communities`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `venues` ADD CONSTRAINT `fk_venues_creator_id` FOREIGN KEY (`creator_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `widgets` ADD CONSTRAINT `fk_widgets_owner` FOREIGN KEY (`owner_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

