CREATE TABLE `payout_requests` (
	`id` int AUTO_INCREMENT NOT NULL,
	`userId` int NOT NULL,
	`amountCents` int NOT NULL,
	`pixKeySnapshotEncrypted` varchar(1024) NOT NULL,
	`status` enum('pending','approved','completed','cancelled') NOT NULL DEFAULT 'pending',
	`processedByUserId` int,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	`processedAt` timestamp,
	CONSTRAINT `payout_requests_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
ALTER TABLE `users` ADD `pixKeyEncrypted` varchar(1024);--> statement-breakpoint
ALTER TABLE `users` ADD `pixKeyType` enum('cpf','cnpj','email','phone','random');--> statement-breakpoint
ALTER TABLE `users` ADD `pixUpdatedAt` timestamp;--> statement-breakpoint
ALTER TABLE `payout_requests` ADD CONSTRAINT `payout_requests_userId_users_id_fk` FOREIGN KEY (`userId`) REFERENCES `users`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `payout_requests` ADD CONSTRAINT `payout_requests_processedByUserId_users_id_fk` FOREIGN KEY (`processedByUserId`) REFERENCES `users`(`id`) ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE INDEX `payout_user_status_idx` ON `payout_requests` (`userId`,`status`,`createdAt`);--> statement-breakpoint
CREATE INDEX `payout_status_created_idx` ON `payout_requests` (`status`,`createdAt`);