-- CreateTable
CREATE TABLE `woc_organizations` (
    `id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `slug` VARCHAR(191) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    UNIQUE INDEX `woc_organizations_slug_key`(`slug`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_users` (
    `id` VARCHAR(191) NOT NULL,
    `organizationId` VARCHAR(191) NULL,
    `name` VARCHAR(191) NULL,
    `email` VARCHAR(191) NOT NULL,
    `passwordHash` VARCHAR(191) NOT NULL,
    `systemRole` ENUM('SYSTEM_ADMIN', 'ORG_ADMIN', 'MANAGER', 'EMPLOYEE') NOT NULL DEFAULT 'EMPLOYEE',
    `status` ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
    `teamId` VARCHAR(191) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    UNIQUE INDEX `woc_users_email_key`(`email`),
    INDEX `woc_users_organizationId_idx`(`organizationId`),
    INDEX `woc_users_teamId_idx`(`teamId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_roles` (
    `id` VARCHAR(191) NOT NULL,
    `organizationId` VARCHAR(191) NULL,
    `name` VARCHAR(191) NOT NULL,
    `key` VARCHAR(191) NOT NULL,
    `systemRole` ENUM('SYSTEM_ADMIN', 'ORG_ADMIN', 'MANAGER', 'EMPLOYEE') NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    INDEX `woc_roles_organizationId_idx`(`organizationId`),
    UNIQUE INDEX `woc_roles_organizationId_key_key`(`organizationId`, `key`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_permissions` (
    `id` VARCHAR(191) NOT NULL,
    `organizationId` VARCHAR(191) NULL,
    `name` VARCHAR(191) NOT NULL,
    `key` VARCHAR(191) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    INDEX `woc_permissions_organizationId_idx`(`organizationId`),
    UNIQUE INDEX `woc_permissions_organizationId_key_key`(`organizationId`, `key`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_teams` (
    `id` VARCHAR(191) NOT NULL,
    `organizationId` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    INDEX `woc_teams_organizationId_idx`(`organizationId`),
    UNIQUE INDEX `woc_teams_organizationId_name_key`(`organizationId`, `name`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_projects` (
    `id` VARCHAR(191) NOT NULL,
    `organizationId` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `isDefault` BOOLEAN NOT NULL DEFAULT false,
    `isArchived` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    INDEX `woc_projects_organizationId_idx`(`organizationId`),
    UNIQUE INDEX `woc_projects_organizationId_name_key`(`organizationId`, `name`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_time_entries` (
    `id` VARCHAR(191) NOT NULL,
    `organizationId` VARCHAR(191) NOT NULL,
    `userId` VARCHAR(191) NOT NULL,
    `projectId` VARCHAR(191) NULL,
    `teamId` VARCHAR(191) NULL,
    `startAt` DATETIME(3) NOT NULL,
    `endAt` DATETIME(3) NULL,
    `durationMinutes` INTEGER NOT NULL DEFAULT 0,
    `isRunning` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    INDEX `woc_time_entries_organizationId_idx`(`organizationId`),
    INDEX `woc_time_entries_userId_idx`(`userId`),
    INDEX `woc_time_entries_projectId_idx`(`projectId`),
    INDEX `woc_time_entries_teamId_idx`(`teamId`),
    INDEX `woc_time_entries_startAt_idx`(`startAt`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_screenshots` (
    `id` VARCHAR(191) NOT NULL,
    `organizationId` VARCHAR(191) NOT NULL,
    `timeEntryId` VARCHAR(191) NOT NULL,
    `userId` VARCHAR(191) NOT NULL,
    `capturedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `objectKey` VARCHAR(255) NOT NULL,
    `localPath` TEXT NULL,
    `previewUrl` TEXT NULL,

    INDEX `woc_screenshots_organizationId_idx`(`organizationId`),
    INDEX `woc_screenshots_userId_idx`(`userId`),
    INDEX `woc_screenshots_timeEntryId_idx`(`timeEntryId`),
    INDEX `woc_screenshots_capturedAt_idx`(`capturedAt`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_adhoc_records` (
    `id` VARCHAR(191) NOT NULL,
    `organizationId` VARCHAR(191) NOT NULL,
    `userId` VARCHAR(191) NOT NULL,
    `date` DATETIME(3) NOT NULL,
    `hours` DECIMAL(5, 2) NOT NULL,
    `reason` TEXT NOT NULL,
    `status` ENUM('pending', 'approved', 'rejected') NOT NULL DEFAULT 'pending',
    `reviewedById` VARCHAR(191) NULL,
    `reviewedAt` DATETIME(3) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    INDEX `woc_adhoc_records_organizationId_idx`(`organizationId`),
    INDEX `woc_adhoc_records_userId_idx`(`userId`),
    INDEX `woc_adhoc_records_status_idx`(`status`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_invite_tokens` (
    `id` VARCHAR(191) NOT NULL,
    `organizationId` VARCHAR(191) NOT NULL,
    `email` VARCHAR(191) NULL,
    `token` VARCHAR(191) NOT NULL,
    `expiresAt` DATETIME(3) NOT NULL,
    `usedAt` DATETIME(3) NULL,
    `createdById` VARCHAR(191) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    UNIQUE INDEX `woc_invite_tokens_token_key`(`token`),
    INDEX `woc_invite_tokens_organizationId_idx`(`organizationId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_password_reset_tokens` (
    `id` VARCHAR(191) NOT NULL,
    `userId` VARCHAR(191) NOT NULL,
    `tokenHash` VARCHAR(191) NOT NULL,
    `expiresAt` DATETIME(3) NOT NULL,
    `usedAt` DATETIME(3) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `woc_password_reset_tokens_tokenHash_key`(`tokenHash`),
    INDEX `woc_password_reset_tokens_userId_idx`(`userId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_organization_settings` (
    `id` VARCHAR(191) NOT NULL,
    `organizationId` VARCHAR(191) NOT NULL,
    `timezone` VARCHAR(191) NOT NULL DEFAULT 'UTC',
    `allowManagersCreateUsers` BOOLEAN NOT NULL DEFAULT false,
    `emailNotificationsEnabled` BOOLEAN NOT NULL DEFAULT true,
    `workingHoursStart` VARCHAR(191) NOT NULL DEFAULT '09:00',
    `workingHoursEnd` VARCHAR(191) NOT NULL DEFAULT '18:00',
    `autoClockOutEnabled` BOOLEAN NOT NULL DEFAULT true,
    `autoClockOutAfterHours` INTEGER NOT NULL DEFAULT 12,
    `idleDetectionMinutes` INTEGER NOT NULL DEFAULT 10,
    `mandatoryClockIn` BOOLEAN NOT NULL DEFAULT true,
    `planName` VARCHAR(191) NOT NULL DEFAULT 'Team',
    `planExpiresAt` DATETIME(3) NULL,
    `subscriptionStatus` ENUM('trial', 'active', 'past_due', 'cancelled', 'expired') NOT NULL DEFAULT 'trial',
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    UNIQUE INDEX `woc_organization_settings_organizationId_key`(`organizationId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_invoices` (
    `id` VARCHAR(191) NOT NULL,
    `organizationId` VARCHAR(191) NOT NULL,
    `number` VARCHAR(191) NOT NULL,
    `amountCents` INTEGER NOT NULL,
    `currency` VARCHAR(191) NOT NULL DEFAULT 'USD',
    `status` ENUM('draft', 'open', 'paid', 'void', 'uncollectible') NOT NULL DEFAULT 'open',
    `issuedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `dueAt` DATETIME(3) NULL,
    `paidAt` DATETIME(3) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    INDEX `woc_invoices_organizationId_idx`(`organizationId`),
    UNIQUE INDEX `woc_invoices_organizationId_number_key`(`organizationId`, `number`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_transactions` (
    `id` VARCHAR(191) NOT NULL,
    `organizationId` VARCHAR(191) NOT NULL,
    `invoiceId` VARCHAR(191) NULL,
    `amountCents` INTEGER NOT NULL,
    `currency` VARCHAR(191) NOT NULL DEFAULT 'USD',
    `status` ENUM('pending', 'success', 'failed', 'refunded') NOT NULL DEFAULT 'success',
    `provider` VARCHAR(191) NOT NULL DEFAULT 'manual',
    `reference` VARCHAR(191) NULL,
    `processedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    INDEX `woc_transactions_organizationId_idx`(`organizationId`),
    INDEX `woc_transactions_invoiceId_idx`(`invoiceId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `woc_audit_logs` (
    `id` VARCHAR(191) NOT NULL,
    `organizationId` VARCHAR(191) NULL,
    `actorUserId` VARCHAR(191) NULL,
    `action` VARCHAR(128) NOT NULL,
    `targetType` VARCHAR(64) NULL,
    `targetId` VARCHAR(191) NULL,
    `metadata` TEXT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `woc_audit_logs_organizationId_idx`(`organizationId`),
    INDEX `woc_audit_logs_actorUserId_idx`(`actorUserId`),
    INDEX `woc_audit_logs_action_idx`(`action`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `_UserRoles` (
    `A` VARCHAR(191) NOT NULL,
    `B` VARCHAR(191) NOT NULL,

    UNIQUE INDEX `_UserRoles_AB_unique`(`A`, `B`),
    INDEX `_UserRoles_B_index`(`B`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `_RolePermissions` (
    `A` VARCHAR(191) NOT NULL,
    `B` VARCHAR(191) NOT NULL,

    UNIQUE INDEX `_RolePermissions_AB_unique`(`A`, `B`),
    INDEX `_RolePermissions_B_index`(`B`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `_ProjectUsers` (
    `A` VARCHAR(191) NOT NULL,
    `B` VARCHAR(191) NOT NULL,

    UNIQUE INDEX `_ProjectUsers_AB_unique`(`A`, `B`),
    INDEX `_ProjectUsers_B_index`(`B`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `woc_users` ADD CONSTRAINT `woc_users_organizationId_fkey` FOREIGN KEY (`organizationId`) REFERENCES `woc_organizations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_users` ADD CONSTRAINT `woc_users_teamId_fkey` FOREIGN KEY (`teamId`) REFERENCES `woc_teams`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_roles` ADD CONSTRAINT `woc_roles_organizationId_fkey` FOREIGN KEY (`organizationId`) REFERENCES `woc_organizations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_permissions` ADD CONSTRAINT `woc_permissions_organizationId_fkey` FOREIGN KEY (`organizationId`) REFERENCES `woc_organizations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_teams` ADD CONSTRAINT `woc_teams_organizationId_fkey` FOREIGN KEY (`organizationId`) REFERENCES `woc_organizations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_projects` ADD CONSTRAINT `woc_projects_organizationId_fkey` FOREIGN KEY (`organizationId`) REFERENCES `woc_organizations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_time_entries` ADD CONSTRAINT `woc_time_entries_organizationId_fkey` FOREIGN KEY (`organizationId`) REFERENCES `woc_organizations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_time_entries` ADD CONSTRAINT `woc_time_entries_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `woc_users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_time_entries` ADD CONSTRAINT `woc_time_entries_projectId_fkey` FOREIGN KEY (`projectId`) REFERENCES `woc_projects`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_screenshots` ADD CONSTRAINT `woc_screenshots_organizationId_fkey` FOREIGN KEY (`organizationId`) REFERENCES `woc_organizations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_screenshots` ADD CONSTRAINT `woc_screenshots_timeEntryId_fkey` FOREIGN KEY (`timeEntryId`) REFERENCES `woc_time_entries`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_screenshots` ADD CONSTRAINT `woc_screenshots_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `woc_users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_adhoc_records` ADD CONSTRAINT `woc_adhoc_records_organizationId_fkey` FOREIGN KEY (`organizationId`) REFERENCES `woc_organizations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_adhoc_records` ADD CONSTRAINT `woc_adhoc_records_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `woc_users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_adhoc_records` ADD CONSTRAINT `woc_adhoc_records_reviewedById_fkey` FOREIGN KEY (`reviewedById`) REFERENCES `woc_users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_invite_tokens` ADD CONSTRAINT `woc_invite_tokens_organizationId_fkey` FOREIGN KEY (`organizationId`) REFERENCES `woc_organizations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_invite_tokens` ADD CONSTRAINT `woc_invite_tokens_createdById_fkey` FOREIGN KEY (`createdById`) REFERENCES `woc_users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_password_reset_tokens` ADD CONSTRAINT `woc_password_reset_tokens_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `woc_users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_organization_settings` ADD CONSTRAINT `woc_organization_settings_organizationId_fkey` FOREIGN KEY (`organizationId`) REFERENCES `woc_organizations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_invoices` ADD CONSTRAINT `woc_invoices_organizationId_fkey` FOREIGN KEY (`organizationId`) REFERENCES `woc_organizations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_transactions` ADD CONSTRAINT `woc_transactions_organizationId_fkey` FOREIGN KEY (`organizationId`) REFERENCES `woc_organizations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_audit_logs` ADD CONSTRAINT `woc_audit_logs_organizationId_fkey` FOREIGN KEY (`organizationId`) REFERENCES `woc_organizations`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `woc_audit_logs` ADD CONSTRAINT `woc_audit_logs_actorUserId_fkey` FOREIGN KEY (`actorUserId`) REFERENCES `woc_users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_UserRoles` ADD CONSTRAINT `_UserRoles_A_fkey` FOREIGN KEY (`A`) REFERENCES `woc_roles`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_UserRoles` ADD CONSTRAINT `_UserRoles_B_fkey` FOREIGN KEY (`B`) REFERENCES `woc_users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_RolePermissions` ADD CONSTRAINT `_RolePermissions_A_fkey` FOREIGN KEY (`A`) REFERENCES `woc_permissions`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_RolePermissions` ADD CONSTRAINT `_RolePermissions_B_fkey` FOREIGN KEY (`B`) REFERENCES `woc_roles`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_ProjectUsers` ADD CONSTRAINT `_ProjectUsers_A_fkey` FOREIGN KEY (`A`) REFERENCES `woc_projects`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_ProjectUsers` ADD CONSTRAINT `_ProjectUsers_B_fkey` FOREIGN KEY (`B`) REFERENCES `woc_users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

