-- Demo only. Import into an empty MySQL 8 database.
SET NAMES utf8mb4;

create table `migrations` (`id` int unsigned not null auto_increment primary key, `migration` varchar(255) not null, `batch` int not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci';
create table `users` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `email` varchar(255) not null, `email_verified_at` timestamp null, `password` varchar(255) not null, `remember_token` varchar(100) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci';
alter table `users` add unique `users_email_unique`(`email`);
create table `password_reset_tokens` (`email` varchar(255) not null, `token` varchar(255) not null, `created_at` timestamp null, primary key (`email`)) default character set utf8mb4 collate 'utf8mb4_unicode_ci';
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';
alter table `sessions` add index `sessions_user_id_index`(`user_id`);
alter table `sessions` add index `sessions_last_activity_index`(`last_activity`);
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';
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';
alter table `cache_locks` add index `cache_locks_expiration_index`(`expiration`);
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';
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';
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';
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`);
alter table `users` add `role` enum('member', 'dealer', 'admin') not null default 'member';
alter table `users` add `username` varchar(32) null;
alter table `users` add `sponsor_id` bigint unsigned null;
alter table `users` add constraint `users_sponsor_id_foreign` foreign key (`sponsor_id`) references `users` (`id`) on delete restrict;
alter table `users` add `mobile` varchar(24) null;
alter table `users` add `gender` varchar(16) null;
alter table `users` add `joining_details` text null;
alter table `users` add unique `users_username_unique`(`username`);
create table `catalog_items` (`id` bigint unsigned not null auto_increment primary key, `sku` varchar(40) not null, `name` varchar(255) not null, `kind` varchar(20) not null, `price_minor` bigint unsigned not null, `active` tinyint(1) not null default '1', `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci';
alter table `catalog_items` add unique `catalog_items_sku_unique`(`sku`);
create table `commerce_orders` (`id` bigint unsigned not null auto_increment primary key, `actor_id` bigint unsigned not null, `buyer_id` bigint unsigned not null, `item_id` bigint unsigned not null, `request_key` char(36) not null, `kind` varchar(20) not null, `status` varchar(20) not null default 'pending', `quantity` int unsigned not null, `unit_minor` bigint unsigned not null, `total_minor` bigint unsigned not null, `approved_at` timestamp null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci';
alter table `commerce_orders` add constraint `commerce_orders_actor_id_foreign` foreign key (`actor_id`) references `users` (`id`) on delete restrict;
alter table `commerce_orders` add constraint `commerce_orders_buyer_id_foreign` foreign key (`buyer_id`) references `users` (`id`) on delete restrict;
alter table `commerce_orders` add constraint `commerce_orders_item_id_foreign` foreign key (`item_id`) references `catalog_items` (`id`) on delete restrict;
alter table `commerce_orders` add unique `commerce_orders_actor_id_request_key_unique`(`actor_id`, `request_key`);
alter table `commerce_orders` add index `commerce_orders_buyer_id_status_kind_index`(`buyer_id`, `status`, `kind`);
create table `dealer_inventory` (`dealer_id` bigint unsigned not null, `item_id` bigint unsigned not null, `quantity` bigint unsigned not null, primary key (`dealer_id`, `item_id`)) default character set utf8mb4 collate 'utf8mb4_unicode_ci';
alter table `dealer_inventory` add constraint `dealer_inventory_dealer_id_foreign` foreign key (`dealer_id`) references `users` (`id`) on delete restrict;
alter table `dealer_inventory` add constraint `dealer_inventory_item_id_foreign` foreign key (`item_id`) references `catalog_items` (`id`) on delete restrict;
create table `order_payments` (`id` bigint unsigned not null auto_increment primary key, `order_id` bigint unsigned not null, `recorded_by` bigint unsigned not null, `reference` varchar(80) not null, `amount_minor` bigint unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci';
alter table `order_payments` add constraint `order_payments_order_id_foreign` foreign key (`order_id`) references `commerce_orders` (`id`) on delete restrict;
alter table `order_payments` add constraint `order_payments_recorded_by_foreign` foreign key (`recorded_by`) references `users` (`id`) on delete restrict;
alter table `order_payments` add unique `order_payments_order_id_reference_unique`(`order_id`, `reference`);
create table `wallet_requests` (`id` bigint unsigned not null auto_increment primary key, `user_id` bigint unsigned not null, `request_key` char(36) not null, `kind` varchar(24) not null, `amount_minor` bigint unsigned not null, `reference` varchar(80) null, `status` varchar(16) not null default 'pending', `reviewed_by` bigint unsigned null, `reviewed_at` timestamp null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci';
alter table `wallet_requests` add constraint `wallet_requests_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete restrict;
alter table `wallet_requests` add constraint `wallet_requests_reviewed_by_foreign` foreign key (`reviewed_by`) references `users` (`id`) on delete restrict;
alter table `wallet_requests` add unique `wallet_requests_user_id_request_key_unique`(`user_id`, `request_key`);
alter table `wallet_requests` add index `wallet_requests_user_id_status_kind_index`(`user_id`, `status`, `kind`);
create table `wallet_entries` (`id` bigint unsigned not null auto_increment primary key, `user_id` bigint unsigned not null, `recorded_by` bigint unsigned not null, `wallet_request_id` bigint unsigned null, `kind` varchar(24) not null, `amount_minor` bigint not null, `reference` varchar(80) not null, `note` varchar(255) null, `created_at` timestamp not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci';
alter table `wallet_entries` add constraint `wallet_entries_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete restrict;
alter table `wallet_entries` add constraint `wallet_entries_recorded_by_foreign` foreign key (`recorded_by`) references `users` (`id`) on delete restrict;
alter table `wallet_entries` add constraint `wallet_entries_wallet_request_id_foreign` foreign key (`wallet_request_id`) references `wallet_requests` (`id`) on delete restrict;
alter table `wallet_entries` add unique `wallet_entries_kind_reference_unique`(`kind`, `reference`);
alter table `wallet_entries` add index `wallet_entries_user_id_kind_index`(`user_id`, `kind`);
alter table `wallet_entries` add unique `wallet_entries_wallet_request_id_unique`(`wallet_request_id`);
ALTER TABLE commerce_orders ADD COLUMN payment_method VARCHAR(24) 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_18_000001_add_role_to_users', 1);
INSERT INTO migrations (migration, batch) VALUES ('2026_11_18_000002_add_joining_details_to_users', 1);
INSERT INTO migrations (migration, batch) VALUES ('2026_11_18_000003_create_commerce_tables', 1);
INSERT INTO migrations (migration, batch) VALUES ('2026_11_18_000004_create_wallet_tables', 1);
INSERT INTO migrations (migration, batch) VALUES ('2026_11_18_000005_add_payment_method_to_commerce_orders', 1);
START TRANSACTION;
INSERT INTO users (id, name, username, email, password, role, sponsor_id, created_at, updated_at) VALUES
(1, 'Demo Admin', 'demo_admin', 'admin@example.test', '$2y$12$3at5gzXcDqcVsjlbNzYT9OzSYRMJYP4jHCJSBG6uCI9Y/jFGPC/5W', 'admin', NULL, NOW(), NOW()),
(2, 'Demo Dealer', 'demo_dealer', 'dealer@example.test', '$2y$12$3at5gzXcDqcVsjlbNzYT9OzSYRMJYP4jHCJSBG6uCI9Y/jFGPC/5W', 'dealer', NULL, NOW(), NOW()),
(3, 'Demo Member', 'demo_member', 'member@example.test', '$2y$12$3at5gzXcDqcVsjlbNzYT9OzSYRMJYP4jHCJSBG6uCI9Y/jFGPC/5W', 'member', 2, NOW(), NOW());
INSERT INTO catalog_items (id, sku, name, kind, price_minor, created_at, updated_at) VALUES
(1, 'D-100', 'Demo Product', 'product', 15000, NOW(), NOW()),
(2, 'D-200', 'Demo Smart Package', 'smart_package', 50000, NOW(), NOW()),
(3, 'D-300', 'Demo Business Package', 'business_package', 100000, NOW(), NOW());
INSERT INTO commerce_orders (actor_id, buyer_id, item_id, request_key, kind, status, quantity, unit_minor, total_minor, approved_at, created_at, updated_at) VALUES (2, 2, 1, UUID(), 'stock', 'approved', 3, 15000, 45000, NOW(), NOW(), NOW());
INSERT INTO dealer_inventory (dealer_id, item_id, quantity) VALUES (2, 1, 3);
INSERT INTO wallet_entries (user_id, recorded_by, kind, amount_minor, reference, created_at) VALUES (3, 1, 'refer_bonus', 25000, 'DEMO-CREDIT', NOW());
COMMIT;
