CREATE TABLE `api_calls` (
	`id` int AUTO_INCREMENT NOT NULL,
	`integrationId` int NOT NULL,
	`endpoint` varchar(255) NOT NULL,
	`method` varchar(10) NOT NULL,
	`status` int NOT NULL,
	`responseTime` int,
	`error` text,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `api_calls_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `bnpl_transactions` (
	`id` int AUTO_INCREMENT NOT NULL,
	`buyerId` int NOT NULL,
	`vendorId` int NOT NULL,
	`amount` decimal(15,2) NOT NULL,
	`status` enum('Pending','Approved','Completed','Cancelled','Defaulted') NOT NULL DEFAULT 'Pending',
	`repaymentSchedule` varchar(50),
	`repaidAmount` decimal(15,2) NOT NULL DEFAULT '0',
	`defaultStatus` varchar(50) DEFAULT 'None',
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `bnpl_transactions_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `bnpl_wallets` (
	`id` int AUTO_INCREMENT NOT NULL,
	`totalBalance` decimal(15,2) NOT NULL DEFAULT '0',
	`availableBalance` decimal(15,2) NOT NULL DEFAULT '0',
	`lockedFunds` decimal(15,2) NOT NULL DEFAULT '0',
	`pendingRepayments` decimal(15,2) NOT NULL DEFAULT '0',
	`defaultedAmount` decimal(15,2) NOT NULL DEFAULT '0',
	`platformFeesHeld` decimal(15,2) NOT NULL DEFAULT '0',
	`utilizationRate` decimal(5,2) NOT NULL DEFAULT '0',
	`reserveRequirement` varchar(20) NOT NULL DEFAULT 'Met',
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `bnpl_wallets_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `buyers` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(255) NOT NULL,
	`email` varchar(320) NOT NULL,
	`phone` varchar(20),
	`businessName` varchar(255),
	`status` enum('Active','Suspended','Revoked') NOT NULL DEFAULT 'Active',
	`creditLimit` decimal(15,2) NOT NULL DEFAULT '0',
	`creditIssued` decimal(15,2) NOT NULL DEFAULT '0',
	`riskLevel` enum('Low','Medium','High') NOT NULL DEFAULT 'Low',
	`creditScore` int,
	`monoLinkedAccounts` int DEFAULT 0,
	`creditchekStatus` varchar(50) DEFAULT 'Pending',
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `buyers_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `escrow_transactions` (
	`id` int AUTO_INCREMENT NOT NULL,
	`transactionId` int,
	`type` enum('Deposit','Hold','Release','Adjustment') NOT NULL,
	`amount` decimal(15,2) NOT NULL,
	`reason` text,
	`initiatedBy` int,
	`status` enum('Pending','Completed','Failed') NOT NULL DEFAULT 'Pending',
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `escrow_transactions_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `integrations` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(100) NOT NULL,
	`type` enum('Mono','Creditchek') NOT NULL,
	`status` enum('Connected','Activated','Disconnected') NOT NULL DEFAULT 'Disconnected',
	`linkedAccounts` int DEFAULT 0,
	`successRate` decimal(5,2) DEFAULT '100',
	`failedRequests` int DEFAULT 0,
	`lastSync` timestamp,
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `integrations_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `repayments` (
	`id` int AUTO_INCREMENT NOT NULL,
	`transactionId` int NOT NULL,
	`buyerId` int NOT NULL,
	`amount` decimal(15,2) NOT NULL,
	`dueDate` timestamp NOT NULL,
	`status` enum('Pending','Completed','Overdue','Defaulted') NOT NULL DEFAULT 'Pending',
	`paymentMethod` varchar(50),
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `repayments_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `vendors` (
	`id` int AUTO_INCREMENT NOT NULL,
	`name` varchar(255) NOT NULL,
	`email` varchar(320) NOT NULL,
	`phone` varchar(20),
	`bankAccount` varchar(50),
	`bankCode` varchar(10),
	`totalPayable` decimal(15,2) NOT NULL DEFAULT '0',
	`settledAmount` decimal(15,2) NOT NULL DEFAULT '0',
	`pendingSettlements` decimal(15,2) NOT NULL DEFAULT '0',
	`failedSettlements` decimal(15,2) NOT NULL DEFAULT '0',
	`status` enum('Active','Suspended','Inactive') NOT NULL DEFAULT 'Active',
	`createdAt` timestamp NOT NULL DEFAULT (now()),
	`updatedAt` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `vendors_id` PRIMARY KEY(`id`)
);
