-- RentNest database structure for MySQL 8.0+ / MariaDB 10.3+ (generated from the Laravel migrations).
-- Import into an EMPTY database (phpMyAdmin: select the database > Import > choose this file > Go).
-- Creates every table and records the migrations as applied, so `php artisan migrate` later only runs new ones.
-- Contains no user data. Your first administrator is created by the app from ADMIN_EMAIL / ADMIN_PASSWORD in .env.

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

CREATE TABLE IF NOT EXISTS `migrations` (`id` int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, `migration` varchar(255) NOT NULL, `batch` int NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 0001_01_01_000000_create_users_table.php
create table `users` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `email` varchar(190) not null, `password` varchar(255) null, `role` varchar(30) not null, `status` varchar(20) not null default 'active', `verified` tinyint(1) not null default '0', `phone` varchar(255) null, `address` varchar(255) null, `city` varchar(255) null, `state` varchar(255) null, `parent_id` bigint unsigned null, `permissions` text null, `payout_bank` varchar(255) null, `payout_account_name` varchar(255) null, `payout_account_number` text null, `oauth_provider` varchar(30) null, `oauth_id` varchar(255) null, `remember_token` varchar(100) null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `users` add unique `users_email_unique`(`email`);
alter table `users` add index `users_role_index`(`role`);
alter table `users` add index `users_parent_id_index`(`parent_id`);
create table `password_reset_tokens` (`email` varchar(190) not null, `token` varchar(255) not null, `created_at` timestamp null, primary key (`email`)) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
create table `sessions` (`id` varchar(255) not null, `user_id` bigint unsigned null, `ip_address` varchar(45) null, `user_agent` text null, `payload` longtext not null, `last_activity` int not null, primary key (`id`)) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `sessions` add index `sessions_user_id_index`(`user_id`);
alter table `sessions` add index `sessions_last_activity_index`(`last_activity`);

-- 0001_01_01_000001_create_cache_table.php
create table `cache` (`key` varchar(255) not null, `value` mediumtext not null, `expiration` bigint not null, primary key (`key`)) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `cache` add index `cache_expiration_index`(`expiration`);
create table `cache_locks` (`key` varchar(255) not null, `owner` varchar(255) not null, `expiration` bigint not null, primary key (`key`)) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `cache_locks` add index `cache_locks_expiration_index`(`expiration`);

-- 0001_01_01_000002_create_jobs_table.php
create table `jobs` (`id` bigint unsigned not null auto_increment primary key, `queue` varchar(255) not null, `payload` longtext not null, `attempts` smallint unsigned not null, `reserved_at` int unsigned null, `available_at` int unsigned not null, `created_at` int unsigned not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `jobs` add index `jobs_queue_index`(`queue`);
create table `job_batches` (`id` varchar(255) not null, `name` varchar(255) not null, `total_jobs` int not null, `pending_jobs` int not null, `failed_jobs` int not null, `failed_job_ids` longtext not null, `options` mediumtext null, `cancelled_at` int null, `created_at` int not null, `finished_at` int null, primary key (`id`)) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
create table `failed_jobs` (`id` bigint unsigned not null auto_increment primary key, `uuid` varchar(255) not null, `connection` varchar(255) not null, `queue` varchar(255) not null, `payload` longtext not null, `exception` longtext not null, `failed_at` timestamp not null default CURRENT_TIMESTAMP) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `failed_jobs` add index `failed_jobs_connection_queue_failed_at_index`(`connection`, `queue`, `failed_at`);
alter table `failed_jobs` add unique `failed_jobs_uuid_unique`(`uuid`);

-- 2026_11_17_000100_create_rentnest_tables.php
create table `contractor_profiles` (`user_id` bigint unsigned not null, `trade` varchar(255) null, `bio` text null, `hourly_rate` int not null default '0', `years_experience` int not null default '0', `rating` double not null default '0', `rating_count` int not null default '0', `jobs_completed` int not null default '0', `available` tinyint(1) not null default '1', primary key (`user_id`)) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
create table `portfolios` (`id` bigint unsigned not null auto_increment primary key, `manager_id` bigint unsigned not null, `name` varchar(255) not null, `client_name` varchar(255) null, `client_email` varchar(255) null, `landlord_id` bigint unsigned null, `description` text null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `portfolios` add index `portfolios_manager_id_index`(`manager_id`);
create table `properties` (`id` bigint unsigned not null auto_increment primary key, `owner_id` bigint unsigned not null, `manager_id` bigint unsigned null, `portfolio_id` bigint unsigned null, `title` varchar(255) not null, `description` text null, `address` varchar(255) not null, `city` varchar(255) not null, `state` varchar(255) not null, `type` varchar(40) not null, `bedrooms` int not null default '0', `bathrooms` int not null default '0', `units` int not null default '1', `rent_amount` bigint not null, `rent_period` varchar(10) not null default 'yearly', `min_stay_months` int not null default '12', `furnished` tinyint(1) not null default '0', `amenities` text null, `images` text null, `status` varchar(20) not null default 'listed', `lat` double null, `lng` double null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `properties` add index `properties_owner_id_index`(`owner_id`);
alter table `properties` add index `properties_manager_id_index`(`manager_id`);
alter table `properties` add index `properties_portfolio_id_index`(`portfolio_id`);
create table `applications` (`id` bigint unsigned not null auto_increment primary key, `property_id` bigint unsigned not null, `tenant_id` bigint unsigned not null, `message` text null, `move_in_date` varchar(10) null, `employment` varchar(255) null, `monthly_income` bigint null, `status` varchar(20) not null default 'pending', `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `applications` add index `applications_property_id_index`(`property_id`);
alter table `applications` add index `applications_tenant_id_index`(`tenant_id`);
create table `leases` (`id` bigint unsigned not null auto_increment primary key, `property_id` bigint unsigned not null, `tenant_id` bigint unsigned not null, `unit_label` varchar(50) null, `start_date` varchar(10) not null, `end_date` varchar(10) not null, `rent_amount` bigint not null, `rent_period` varchar(10) not null, `status` varchar(30) not null default 'pending_signature', `agreement_text` longtext null, `signed_at` datetime null, `signature_name` varchar(255) null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `leases` add index `leases_property_id_index`(`property_id`);
alter table `leases` add index `leases_tenant_id_index`(`tenant_id`);
create table `rents` (`id` bigint unsigned not null auto_increment primary key, `lease_id` bigint unsigned not null, `tenant_id` bigint unsigned not null, `property_id` bigint unsigned not null, `period_label` varchar(40) null, `due_date` varchar(10) not null, `amount` bigint not null, `status` varchar(20) not null default 'due', `paid_at` datetime null, `payment_id` bigint unsigned null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `rents` add index `rents_lease_id_index`(`lease_id`);
alter table `rents` add index `rents_tenant_id_index`(`tenant_id`);
alter table `rents` add index `rents_property_id_index`(`property_id`);
create table `payments` (`id` bigint unsigned not null auto_increment primary key, `payer_id` bigint unsigned not null, `payee_id` bigint unsigned not null, `kind` varchar(20) not null, `ref_id` bigint unsigned not null, `amount` bigint not null, `gateway` varchar(30) not null, `reference` varchar(80) not null, `status` varchar(20) not null default 'pending', `receipt_no` varchar(40) null, `description` varchar(255) null, `paid_at` datetime null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `payments` add index `payments_payer_id_index`(`payer_id`);
alter table `payments` add index `payments_payee_id_index`(`payee_id`);
alter table `payments` add unique `payments_reference_unique`(`reference`);
create table `maintenance_requests` (`id` bigint unsigned not null auto_increment primary key, `property_id` bigint unsigned not null, `created_by` bigint unsigned not null, `title` varchar(255) not null, `description` text null, `category` varchar(40) null, `priority` varchar(20) not null default 'normal', `status` varchar(20) not null default 'open', `images` text null, `contractor_id` bigint unsigned null, `agreed_cost` bigint null, `open_for_bids` tinyint(1) not null default '0', `rating` int null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `maintenance_requests` add index `maintenance_requests_property_id_index`(`property_id`);
alter table `maintenance_requests` add index `maintenance_requests_contractor_id_index`(`contractor_id`);
create table `bids` (`id` bigint unsigned not null auto_increment primary key, `request_id` bigint unsigned not null, `contractor_id` bigint unsigned not null, `amount` bigint not null, `message` text null, `status` varchar(20) not null default 'pending', `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `bids` add index `bids_request_id_index`(`request_id`);
create table `job_updates` (`id` bigint unsigned not null auto_increment primary key, `request_id` bigint unsigned not null, `user_id` bigint unsigned not null, `note` text null, `images` text null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `job_updates` add index `job_updates_request_id_index`(`request_id`);
create table `payment_requests` (`id` bigint unsigned not null auto_increment primary key, `request_id` bigint unsigned not null, `contractor_id` bigint unsigned not null, `landlord_id` bigint unsigned not null, `amount` bigint not null, `note` text null, `status` varchar(20) not null default 'pending', `payment_id` bigint unsigned null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `payment_requests` add index `payment_requests_request_id_index`(`request_id`);
alter table `payment_requests` add index `payment_requests_contractor_id_index`(`contractor_id`);
alter table `payment_requests` add index `payment_requests_landlord_id_index`(`landlord_id`);
create table `documents` (`id` bigint unsigned not null auto_increment primary key, `owner_id` bigint unsigned not null, `property_id` bigint unsigned null, `application_id` bigint unsigned null, `kind` varchar(40) null, `name` varchar(255) not null, `path` varchar(255) not null, `mime` varchar(80) null, `size` int null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `documents` add index `documents_owner_id_index`(`owner_id`);
create table `notices` (`id` bigint unsigned not null auto_increment primary key, `lease_id` bigint unsigned not null, `property_id` bigint unsigned not null, `tenant_id` bigint unsigned not null, `issued_by` bigint unsigned not null, `reason` text null, `vacate_by` varchar(10) null, `status` varchar(20) not null default 'issued', `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `notices` add index `notices_lease_id_index`(`lease_id`);
alter table `notices` add index `notices_tenant_id_index`(`tenant_id`);
create table `conversations` (`id` bigint unsigned not null auto_increment primary key, `type` varchar(20) not null, `property_id` bigint unsigned null, `listing_id` bigint unsigned null, `request_id` bigint unsigned null, `user_a` bigint unsigned not null, `user_b` bigint unsigned not null, `archived` tinyint(1) not null default '0', `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `conversations` add index `conversations_user_a_index`(`user_a`);
alter table `conversations` add index `conversations_user_b_index`(`user_b`);
create table `messages` (`id` bigint unsigned not null auto_increment primary key, `conversation_id` bigint unsigned not null, `sender_id` bigint unsigned not null, `body` text not null, `hidden` tinyint(1) not null default '0', `read_at` datetime null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `messages` add index `messages_conversation_id_index`(`conversation_id`);
create table `forum_posts` (`id` bigint unsigned not null auto_increment primary key, `user_id` bigint unsigned not null, `city` varchar(255) null, `state` varchar(255) null, `category` varchar(40) null, `title` varchar(255) not null, `body` text null, `hidden` tinyint(1) not null default '0', `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
create table `forum_comments` (`id` bigint unsigned not null auto_increment primary key, `post_id` bigint unsigned not null, `user_id` bigint unsigned not null, `body` text not null, `hidden` tinyint(1) not null default '0', `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `forum_comments` add index `forum_comments_post_id_index`(`post_id`);
create table `listings` (`id` bigint unsigned not null auto_increment primary key, `seller_id` bigint unsigned not null, `title` varchar(255) not null, `description` text null, `price` bigint not null, `category` varchar(40) null, `item_condition` varchar(20) null, `city` varchar(255) null, `state` varchar(255) null, `images` text null, `status` varchar(20) not null default 'active', `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `listings` add index `listings_seller_id_index`(`seller_id`);
create table `audit_logs` (`id` bigint unsigned not null auto_increment primary key, `actor_id` bigint unsigned null, `action` varchar(60) not null, `entity` varchar(40) null, `entity_id` bigint unsigned null, `detail` text null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
create table `settings` (`name` varchar(100) not null, `value` text null, primary key (`name`)) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
create table `notifications` (`id` bigint unsigned not null auto_increment primary key, `user_id` bigint unsigned not null, `title` varchar(255) not null, `body` text null, `link` varchar(255) null, `read_at` datetime null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `notifications` add index `notifications_user_id_index`(`user_id`);

-- 2026_11_17_213932_create_personal_access_tokens_table.php
create table `personal_access_tokens` (`id` bigint unsigned not null auto_increment primary key, `tokenable_type` varchar(255) not null, `tokenable_id` bigint unsigned not null, `name` text not null, `token` varchar(64) not null, `abilities` text null, `last_used_at` timestamp null, `expires_at` timestamp null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `personal_access_tokens` add index `personal_access_tokens_tokenable_type_tokenable_id_index`(`tokenable_type`, `tokenable_id`);
alter table `personal_access_tokens` add unique `personal_access_tokens_token_unique`(`token`);
alter table `personal_access_tokens` add index `personal_access_tokens_expires_at_index`(`expires_at`);

-- 2026_11_20_000100_add_account_email_and_job_runs.php
alter table `users` add `first_name` varchar(100) null;
alter table `users` add `last_name` varchar(100) null;
alter table `users` add `email_verified_at` datetime null;
create table `job_runs` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(60) not null, `started_at` datetime not null, `finished_at` datetime null, `status` varchar(10) not null default 'running', `counters` text null, `error` text null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `job_runs` add index `job_runs_name_index`(`name`);

-- 2026_11_21_000100_add_billing.php
alter table `rents` add `kind` varchar(20) not null default 'rent';
alter table `rents` add `note` varchar(255) null;
alter table `rents` add `late_fee_for` bigint unsigned null;
alter table `rents` add `plan_id` bigint unsigned null;
alter table `rents` add `created_by` bigint unsigned null;
alter table `rents` add `overdue_at` datetime null;
alter table `rents` add `upcoming_reminded_at` datetime null;
alter table `rents` add `overdue_reminded_at` datetime null;
alter table `rents` add index `rents_status_due_date_index`(`status`, `due_date`);
alter table `rents` add unique `rents_late_fee_for_unique`(`late_fee_for`);
alter table `rents` add index `rents_plan_id_index`(`plan_id`);
alter table `leases` add `deposit_amount` bigint not null default '0';
create table `payment_plans` (`id` bigint unsigned not null auto_increment primary key, `lease_id` bigint unsigned not null, `tenant_id` bigint unsigned not null, `landlord_id` bigint unsigned not null, `property_id` bigint unsigned not null, `total_amount` bigint not null, `instalments` int not null, `instalment_amount` bigint not null, `first_due_date` varchar(10) not null, `reason` text null, `status` varchar(20) not null default 'pending', `landlord_note` text null, `covered_ids` text null, `created_ids` text null, `decided_at` datetime null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `payment_plans` add index `payment_plans_lease_id_index`(`lease_id`);
alter table `payment_plans` add index `payment_plans_tenant_id_index`(`tenant_id`);
alter table `payment_plans` add index `payment_plans_landlord_id_index`(`landlord_id`);
create table `billing_settings` (`user_id` bigint unsigned not null, `late_fees` tinyint(1) not null default '1', `late_fee_percent` double not null default '5', `grace_days` int not null default '5', `send_upcoming` tinyint(1) not null default '1', `remind_days_before` int not null default '3', `send_overdue` tinyint(1) not null default '1', `overdue_every_days` int not null default '3', `email_tenants` tinyint(1) not null default '1', `notify_me` tinyint(1) not null default '0', `updated_at` datetime null, primary key (`user_id`)) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;

-- 2026_11_22_000100_add_leasing.php
alter table `applications` add `details` text null;
alter table `applications` add `screening` text null;
alter table `applications` add `landlord_notes` text null;
alter table `applications` add `reviewed_at` datetime null;
create table `lease_templates` (`id` bigint unsigned not null auto_increment primary key, `owner_id` bigint unsigned not null, `name` varchar(120) not null, `body` longtext not null, `created_at` datetime null, `updated_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `lease_templates` add index `lease_templates_owner_id_index`(`owner_id`);
alter table `leases` modify `rent_period` varchar(20) not null;
alter table `leases` add `template_id` bigint unsigned null;
alter table `leases` add `tenant_signature` longtext null;
alter table `leases` add `tenant_sign_ip` varchar(45) null;
alter table `leases` add `landlord_signature` longtext null;
alter table `leases` add `landlord_signature_name` varchar(255) null;
alter table `leases` add `landlord_signed_at` datetime null;
alter table `leases` add `landlord_signer_id` bigint unsigned null;
alter table `leases` add `landlord_sign_ip` varchar(45) null;
alter table `leases` add `sign_token_hash` varchar(64) null;
alter table `leases` add `sign_token_expires_at` datetime null;
alter table `leases` add `document_id` bigint unsigned null;
alter table `leases` add `renewed_from` bigint unsigned null;
alter table `leases` add `amendments` text null;
alter table `leases` add `expiry_alert_sent` tinyint(1) not null default '0';
alter table `leases` add index `leases_status_end_date_index`(`status`, `end_date`);
alter table `leases` add index `leases_sign_token_hash_index`(`sign_token_hash`);
alter table `leases` add index `leases_renewed_from_index`(`renewed_from`);
alter table `notices` add `type` varchar(20) not null default 'quit';

-- 2026_11_23_000100_add_listings.php
alter table `properties` add `category` varchar(20) not null default 'for_rent';
alter table `properties` add `purchase_price` bigint null;
alter table `properties` add `area` varchar(120) null;
alter table `properties` add `serviced` tinyint(1) not null default '0';
alter table `properties` add `size_sqm` int null;
alter table `properties` add `year_built` int null;
alter table `properties` add `parking_spaces` int null;
alter table `properties` add `pet_policy` varchar(20) null;
alter table `properties` add `virtual_tour_url` varchar(500) null;
alter table `properties` add `floor_plans` text null;
alter table `properties` add `featured` tinyint(1) not null default '0';
alter table `properties` add `listing_verified` tinyint(1) not null default '0';
alter table `properties` add index `properties_status_category_index`(`status`, `category`);
alter table `properties` add index `properties_state_city_index`(`state`, `city`);
alter table `properties` add index `properties_lat_lng_index`(`lat`, `lng`);
create table `property_units` (`id` bigint unsigned not null auto_increment primary key, `property_id` bigint unsigned not null, `label` varchar(50) not null, `bedrooms` int null, `bathrooms` int null, `rent_amount` bigint null, `size_sqm` int null, `status` varchar(20) not null default 'available', `notes` text null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `property_units` add unique `property_units_property_id_label_unique`(`property_id`, `label`);
alter table `property_units` add index `property_units_property_id_index`(`property_id`);
create table `saved_searches` (`id` bigint unsigned not null auto_increment primary key, `user_id` bigint unsigned not null, `name` varchar(120) not null, `filters` text not null, `notify` tinyint(1) not null default '1', `last_checked_at` datetime null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `saved_searches` add index `saved_searches_user_id_index`(`user_id`);
create table `property_views` (`id` bigint unsigned not null auto_increment primary key, `property_id` bigint unsigned not null, `viewer_id` bigint unsigned null, `visitor` varchar(64) not null, `source` varchar(20) not null default 'search', `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `property_views` add index `property_views_property_id_created_at_index`(`property_id`, `created_at`);
alter table `property_views` add unique `property_views_property_id_visitor_unique`(`property_id`, `visitor`);

-- 2026_11_24_000100_add_payouts.php
alter table `users` add `payout_type` varchar(20) null;
alter table `users` add `payout_bank_code` varchar(20) null;
alter table `users` add `payout_currency` varchar(8) null;
alter table `users` add `paystack_recipient_code` varchar(100) null;
alter table `users` add `payout_verified_at` datetime null;
alter table `portfolios` add `commission_type` varchar(10) not null default 'none';
alter table `portfolios` add `commission_value` double not null default '0';
alter table `payments` add `split_info` text null;
alter table `payment_requests` add `kind` varchar(20) not null default 'completion';
alter table `payment_requests` add `percentage` int null;
create table `payouts` (`id` bigint unsigned not null auto_increment primary key, `payment_id` bigint unsigned not null, `user_id` bigint unsigned not null, `role` varchar(20) not null, `amount` bigint not null, `status` varchar(20) not null default 'pending', `reference` varchar(80) null, `transfer_code` varchar(100) null, `attempts` int not null default '0', `note` varchar(255) null, `created_at` datetime null, `updated_at` datetime null, `completed_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `payouts` add unique `payouts_payment_id_user_id_role_unique`(`payment_id`, `user_id`, `role`);
alter table `payouts` add index `payouts_status_updated_at_index`(`status`, `updated_at`);
alter table `payouts` add index `payouts_user_id_index`(`user_id`);
alter table `payouts` add unique `payouts_reference_unique`(`reference`);

-- 2026_11_25_000100_add_maintenance_extras.php
alter table `maintenance_requests` add `scheduled_at` datetime null;
alter table `maintenance_requests` add `schedule_reminded_at` datetime null;
alter table `maintenance_requests` add `calendar_seq` int not null default '0';
alter table `maintenance_requests` add index `maintenance_requests_scheduled_at_index`(`scheduled_at`);
alter table `users` add `calendar_token_hash` varchar(64) null;
alter table `users` add `calendar_token` text null;
alter table `users` add unique `users_calendar_token_hash_unique`(`calendar_token_hash`);
create table `expenses` (`id` bigint unsigned not null auto_increment primary key, `account_id` bigint unsigned not null, `property_id` bigint unsigned not null, `request_id` bigint unsigned null, `payment_id` bigint unsigned null, `category` varchar(30) not null, `amount` bigint not null, `expense_date` varchar(10) not null, `vendor` varchar(150) null, `description` varchar(500) null, `receipt_document_id` bigint unsigned null, `created_by` bigint unsigned not null, `created_at` datetime null, `updated_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `expenses` add index `expenses_property_id_expense_date_index`(`property_id`, `expense_date`);
alter table `expenses` add index `expenses_account_id_index`(`account_id`);
alter table `expenses` add index `expenses_request_id_index`(`request_id`);
alter table `expenses` add unique `expenses_payment_id_unique`(`payment_id`);
alter table `maintenance_requests` modify `priority` varchar(20) not null default 'medium';

-- 2026_11_26_000100_add_inspections.php
create table `inspection_templates` (`id` bigint unsigned not null auto_increment primary key, `account_id` bigint unsigned not null, `name` varchar(120) not null, `type` varchar(16) not null default 'routine', `description` varchar(1000) null, `areas` text not null, `is_default` tinyint(1) not null default '0', `created_at` datetime null, `updated_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `inspection_templates` add index `inspection_templates_account_id_index`(`account_id`);
create table `inspections` (`id` bigint unsigned not null auto_increment primary key, `property_id` bigint unsigned not null, `lease_id` bigint unsigned not null, `tenant_id` bigint unsigned not null, `created_by` bigint unsigned not null, `title` varchar(150) not null, `description` varchar(2000) null, `type` varchar(16) not null default 'routine', `scheduled_date` varchar(10) not null, `status` varchar(16) not null default 'scheduled', `areas` text not null, `results` text null, `tenant_notes` varchar(2000) null, `submitted_at` datetime null, `rating` varchar(20) null, `feedback` varchar(2000) null, `reviewed_at` datetime null, `reviewed_by` bigint unsigned null, `rounds` int not null default '0', `reminded_at` datetime null, `overdue_at` datetime null, `created_at` datetime null, `updated_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `inspections` add index `inspections_status_scheduled_date_index`(`status`, `scheduled_date`);
alter table `inspections` add index `inspections_property_id_index`(`property_id`);
alter table `inspections` add index `inspections_lease_id_index`(`lease_id`);
alter table `inspections` add index `inspections_tenant_id_index`(`tenant_id`);

-- 2026_11_27_000100_add_admin_hardening.php
alter table `audit_logs` add `ip` varchar(45) null;
alter table `audit_logs` add `user_agent` varchar(255) null;
alter table `audit_logs` add `changes` text null;
alter table `audit_logs` add index `audit_logs_entity_entity_id_index`(`entity`, `entity_id`);
alter table `leases` add `ended_on` varchar(10) null;

-- 2026_11_28_000100_add_community.php
create table `blog_posts` (`id` bigint unsigned not null auto_increment primary key, `author_id` bigint unsigned not null, `title` varchar(200) not null, `slug` varchar(220) not null, `excerpt` varchar(400) null, `body` text not null, `cover_image` varchar(255) null, `tags` varchar(500) null, `status` varchar(16) not null default 'draft', `published_at` datetime null, `created_at` datetime null, `updated_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `blog_posts` add index `blog_posts_status_published_at_index`(`status`, `published_at`);
alter table `blog_posts` add unique `blog_posts_slug_unique`(`slug`);
alter table `forum_posts` add `tags` varchar(300) null;
alter table `forum_posts` add `pinned` tinyint(1) not null default '0';
alter table `forum_posts` add `score` int not null default '0';
create table `forum_votes` (`id` bigint unsigned not null auto_increment primary key, `post_id` bigint unsigned not null, `user_id` bigint unsigned not null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `forum_votes` add unique `forum_votes_post_id_user_id_unique`(`post_id`, `user_id`);
alter table `listings` add `negotiable` tinyint(1) not null default '0';
alter table `listings` add `pickup_only` tinyint(1) not null default '0';
alter table `listings` add `accept_payment` tinyint(1) not null default '0';
alter table `listings` add `delist_reason` varchar(500) null;
alter table `listings` add `buyer_id` bigint unsigned null;
alter table `listings` add `reserved_by` bigint unsigned null;
alter table `listings` add `reserved_until` datetime null;
alter table `listings` add `sold_at` datetime null;
alter table `listings` add `updated_at` datetime null;
alter table `messages` add `reply_to` bigint unsigned null;
alter table `messages` add `attachments` text null;
alter table `messages` add `edited_at` datetime null;
alter table `messages` add `deleted_at` datetime null;
create table `saved_homes` (`id` bigint unsigned not null auto_increment primary key, `user_id` bigint unsigned not null, `property_id` bigint unsigned not null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `saved_homes` add unique `saved_homes_user_id_property_id_unique`(`user_id`, `property_id`);
alter table `saved_homes` add index `saved_homes_property_id_index`(`property_id`);

-- 2026_11_29_000100_add_crm.php
create table `crm_clients` (`id` bigint unsigned not null auto_increment primary key, `agent_id` bigint unsigned not null, `user_id` bigint unsigned null, `name` varchar(150) not null, `email` varchar(190) null, `phone` varchar(50) null, `type` varchar(16) not null default 'buyer', `stage` varchar(16) not null default 'lead', `source` varchar(16) not null default 'manual', `budget_min` bigint null, `budget_max` bigint null, `preferences` text null, `stage_changed_at` datetime null, `last_contact_at` datetime null, `unsubscribed_at` datetime null, `created_at` datetime null, `updated_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `crm_clients` add index `crm_clients_agent_id_email_index`(`agent_id`, `email`);
alter table `crm_clients` add index `crm_clients_agent_id_index`(`agent_id`);
alter table `crm_clients` add index `crm_clients_user_id_index`(`user_id`);
create table `crm_notes` (`id` bigint unsigned not null auto_increment primary key, `client_id` bigint unsigned not null, `author_id` bigint unsigned not null, `kind` varchar(16) not null default 'note', `body` text not null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `crm_notes` add index `crm_notes_client_id_index`(`client_id`);
create table `crm_tasks` (`id` bigint unsigned not null auto_increment primary key, `agent_id` bigint unsigned not null, `client_id` bigint unsigned null, `title` varchar(200) not null, `due_date` varchar(10) null, `priority` varchar(8) not null default 'normal', `done_at` datetime null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `crm_tasks` add index `crm_tasks_agent_id_index`(`agent_id`);
alter table `crm_tasks` add index `crm_tasks_client_id_index`(`client_id`);
create table `crm_retainers` (`id` bigint unsigned not null auto_increment primary key, `agent_id` bigint unsigned not null, `client_id` bigint unsigned not null, `payer_id` bigint unsigned null, `amount` bigint not null, `description` varchar(300) not null, `status` varchar(12) not null default 'pending', `payment_id` bigint unsigned null, `paid_at` datetime null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `crm_retainers` add index `crm_retainers_agent_id_index`(`agent_id`);
alter table `crm_retainers` add index `crm_retainers_client_id_index`(`client_id`);
alter table `crm_retainers` add index `crm_retainers_payer_id_index`(`payer_id`);
create table `agent_settings` (`agent_id` bigint unsigned not null, `slug` varchar(60) not null, `booking_open` tinyint(1) not null default '0', `weekly` text not null, `slot_minutes` smallint unsigned not null default '60', `daily_limit` smallint unsigned not null default '4', `notice_hours` smallint unsigned not null default '12', `blocked_dates` text null, `showcase` text null, `intro` varchar(500) null, `updated_at` datetime null, primary key (`agent_id`)) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `agent_settings` add unique `agent_settings_slug_unique`(`slug`);
create table `tours` (`id` bigint unsigned not null auto_increment primary key, `agent_id` bigint unsigned not null, `client_id` bigint unsigned null, `property_id` bigint unsigned null, `starts_at` datetime not null, `ends_at` datetime not null, `status` varchar(12) not null default 'confirmed', `name` varchar(150) not null, `email` varchar(190) not null, `phone` varchar(50) null, `message` varchar(1000) null, `source` varchar(12) not null default 'booking', `cancel_token` varchar(64) null, `calendar_seq` smallint unsigned not null default '0', `confirmed_sent_at` datetime null, `completed_at` datetime null, `followup_sent_at` datetime null, `created_at` datetime null, `updated_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `tours` add index `tours_agent_id_starts_at_index`(`agent_id`, `starts_at`);
alter table `tours` add index `tours_agent_id_index`(`agent_id`);
alter table `tours` add index `tours_client_id_index`(`client_id`);
alter table `tours` add unique `tours_cancel_token_unique`(`cancel_token`);
create table `crm_templates` (`id` bigint unsigned not null auto_increment primary key, `agent_id` bigint unsigned not null, `name` varchar(120) not null, `subject` varchar(200) not null, `body` text not null, `created_at` datetime null, `updated_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `crm_templates` add index `crm_templates_agent_id_index`(`agent_id`);
create table `crm_sequences` (`id` bigint unsigned not null auto_increment primary key, `agent_id` bigint unsigned not null, `name` varchar(120) not null, `trigger_stage` varchar(16) not null, `active` tinyint(1) not null default '1', `steps` text not null, `created_at` datetime null, `updated_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `crm_sequences` add index `crm_sequences_agent_id_index`(`agent_id`);
create table `crm_enrollments` (`id` bigint unsigned not null auto_increment primary key, `sequence_id` bigint unsigned not null, `client_id` bigint unsigned not null, `step` smallint unsigned not null default '0', `next_at` datetime null, `status` varchar(10) not null default 'active', `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `crm_enrollments` add unique `crm_enrollments_sequence_id_client_id_unique`(`sequence_id`, `client_id`);
alter table `crm_enrollments` add index `crm_enrollments_status_next_at_index`(`status`, `next_at`);
alter table `crm_enrollments` add index `crm_enrollments_client_id_index`(`client_id`);
create table `crm_campaigns` (`id` bigint unsigned not null auto_increment primary key, `agent_id` bigint unsigned not null, `template_id` bigint unsigned not null, `name` varchar(120) not null, `audience` text not null, `status` varchar(10) not null default 'draft', `recipients` int unsigned not null default '0', `sent_at` datetime null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `crm_campaigns` add index `crm_campaigns_agent_id_index`(`agent_id`);
create table `crm_emails` (`id` bigint unsigned not null auto_increment primary key, `agent_id` bigint unsigned not null, `client_id` bigint unsigned not null, `template_id` bigint unsigned null, `campaign_id` bigint unsigned null, `sequence_id` bigint unsigned null, `kind` varchar(10) not null default 'single', `subject` varchar(200) not null, `body` text not null, `status` varchar(10) not null default 'queued', `error` varchar(300) null, `clicks` int unsigned not null default '0', `first_click_at` datetime null, `sent_at` datetime null, `created_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `crm_emails` add index `crm_emails_status_id_index`(`status`, `id`);
alter table `crm_emails` add index `crm_emails_agent_id_index`(`agent_id`);
alter table `crm_emails` add index `crm_emails_client_id_index`(`client_id`);
alter table `crm_emails` add index `crm_emails_campaign_id_index`(`campaign_id`);

-- 2026_11_30_000100_add_plans.php
create table `plans` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(80) not null, `description` varchar(500) null, `roles` text not null, `modules` text not null, `price_monthly` bigint null, `price_yearly` bigint null, `ps_monthly_code` varchar(40) null, `ps_monthly_amount` bigint null, `ps_yearly_code` varchar(40) null, `ps_yearly_amount` bigint null, `highlight` tinyint(1) not null default '0', `active` tinyint(1) not null default '1', `sort` int not null default '0', `created_at` datetime null, `updated_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
create table `subscriptions` (`id` bigint unsigned not null auto_increment primary key, `user_id` bigint unsigned not null, `plan_id` bigint unsigned not null, `interval` varchar(8) not null, `gateway` varchar(16) not null, `status` varchar(14) not null default 'pending', `current_period_end` datetime null, `ps_subscription_code` varchar(60) null, `ps_email_token` varchar(60) null, `ps_customer_code` varchar(60) null, `reminded_at` datetime null, `created_at` datetime null, `updated_at` datetime null) default character set utf8mb4 collate 'utf8mb4_unicode_ci' engine = InnoDB;
alter table `subscriptions` add index `subscriptions_user_id_index`(`user_id`);
alter table `subscriptions` add index `subscriptions_ps_subscription_code_index`(`ps_subscription_code`);
alter table `users` add `plan_id` bigint unsigned null;
alter table `users` add `plan_until` varchar(10) null;

INSERT INTO `migrations` (`migration`, `batch`) VALUES ('0001_01_01_000000_create_users_table', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('0001_01_01_000001_create_cache_table', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('0001_01_01_000002_create_jobs_table', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('2026_11_17_000100_create_rentnest_tables', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('2026_11_17_213932_create_personal_access_tokens_table', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('2026_11_20_000100_add_account_email_and_job_runs', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('2026_11_21_000100_add_billing', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('2026_11_22_000100_add_leasing', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('2026_11_23_000100_add_listings', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('2026_11_24_000100_add_payouts', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('2026_11_25_000100_add_maintenance_extras', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('2026_11_26_000100_add_inspections', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('2026_11_27_000100_add_admin_hardening', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('2026_11_28_000100_add_community', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('2026_11_29_000100_add_crm', 1);
INSERT INTO `migrations` (`migration`, `batch`) VALUES ('2026_11_30_000100_add_plans', 1);

SET FOREIGN_KEY_CHECKS = 1;
