CREATE TABLE IF NOT EXISTS `credit_payments` (
  `id` text PRIMARY KEY NOT NULL,
  `customer_id` text NOT NULL,
  `credits` integer NOT NULL,
  `amount_minor` integer NOT NULL,
  `currency` text DEFAULT 'USD' NOT NULL,
  `provider` text DEFAULT 'PAYTR' NOT NULL,
  `status` text DEFAULT 'PENDING' NOT NULL,
  `test_mode` integer DEFAULT 1 NOT NULL,
  `provider_payment_type` text,
  `provider_total_minor` integer,
  `failure_code` text,
  `failure_message` text,
  `created_at` integer NOT NULL,
  `updated_at` integer NOT NULL,
  `paid_at` integer,
  FOREIGN KEY (`customer_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `idx_credit_payments_customer_created` ON `credit_payments` (`customer_id`,`created_at`);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `idx_credit_payments_status_created` ON `credit_payments` (`status`,`created_at`);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS `credit_payment_grants` (
  `payment_id` text PRIMARY KEY NOT NULL,
  `wallet_id` text NOT NULL,
  `credits` integer NOT NULL,
  `applied_at` integer,
  `created_at` integer NOT NULL,
  FOREIGN KEY (`payment_id`) REFERENCES `credit_payments`(`id`) ON UPDATE no action ON DELETE no action,
  FOREIGN KEY (`wallet_id`) REFERENCES `credit_wallets`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `idx_credit_payment_grants_wallet` ON `credit_payment_grants` (`wallet_id`);
