CREATE TABLE IF NOT EXISTS `users` (
  `id` text PRIMARY KEY NOT NULL,
  `email` text NOT NULL,
  `name` text NOT NULL,
  `role` text NOT NULL,
  `status` text DEFAULT 'ACTIVE' NOT NULL,
  `phone` text,
  `company` text,
  `avatar_url` text,
  `created_at` integer NOT NULL,
  `updated_at` integer NOT NULL,
  `deleted_at` integer
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS `idx_users_email` ON `users` (`email`);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `idx_users_role_status` ON `users` (`role`,`status`);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS `credit_wallets` (
  `id` text PRIMARY KEY NOT NULL,
  `user_id` text NOT NULL,
  `available` integer DEFAULT 0 NOT NULL,
  `reserved` integer DEFAULT 0 NOT NULL,
  `version` integer DEFAULT 0 NOT NULL,
  `created_at` integer NOT NULL,
  `updated_at` integer NOT NULL,
  `deleted_at` integer,
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS `idx_wallet_user` ON `credit_wallets` (`user_id`);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS `auth_accounts` (
  `id` text PRIMARY KEY NOT NULL,
  `user_id` text NOT NULL,
  `provider` text NOT NULL,
  `provider_subject` text,
  `password_hash` text,
  `password_salt` text,
  `created_at` integer NOT NULL,
  `updated_at` integer NOT NULL,
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS `idx_auth_provider_subject` ON `auth_accounts` (`provider`,`provider_subject`);

--> statement-breakpoint
CREATE TABLE IF NOT EXISTS `auth_sessions` (
  `id` text PRIMARY KEY NOT NULL,
  `user_id` text NOT NULL,
  `token_hash` text NOT NULL,
  `expires_at` integer NOT NULL,
  `created_at` integer NOT NULL,
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
);
--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS `idx_auth_sessions_token` ON `auth_sessions` (`token_hash`);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `idx_auth_sessions_user_expiry` ON `auth_sessions` (`user_id`,`expires_at`);
--> statement-breakpoint
ALTER TABLE `demo_customer_requests` ADD COLUMN `customer_user_id` text REFERENCES `users`(`id`);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `idx_demo_requests_customer_user` ON `demo_customer_requests` (`customer_user_id`,`created_at`);
--> statement-breakpoint
PRAGMA optimize;
