-- CreateTable
CREATE TABLE `attendance` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `user_id` INTEGER NOT NULL,
    `shift_date` DATE NOT NULL,
    `arrival_time` DATETIME(0) NULL,
    `arrival_lat` DECIMAL(10, 8) NULL,
    `arrival_lng` DECIMAL(11, 8) NULL,
    `arrival_accuracy` DECIMAL(10, 2) NULL,
    `arrival_address` VARCHAR(500) NULL,
    `break_start` DATETIME(0) NULL,
    `break_end` DATETIME(0) NULL,
    `departure_time` DATETIME(0) NULL,
    `departure_lat` DECIMAL(10, 8) NULL,
    `departure_lng` DECIMAL(11, 8) NULL,
    `departure_accuracy` DECIMAL(10, 2) NULL,
    `departure_address` VARCHAR(500) NULL,
    `notes` TEXT NULL,
    `project_id` INTEGER NULL,
    `leave_type` ENUM('work', 'vacation', 'sick', 'holiday', 'unpaid') NULL DEFAULT 'work',
    `leave_hours` DECIMAL(4, 2) NULL,
    `created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),

    INDEX `idx_attendance_user_date`(`user_id`, `shift_date`),
    INDEX `idx_attendance_user_departure`(`user_id`, `departure_time`),
    INDEX `idx_project_id`(`project_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `attendance_project_logs` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `attendance_id` INTEGER NOT NULL,
    `project_id` INTEGER NOT NULL,
    `started_at` DATETIME(0) NULL,
    `ended_at` DATETIME(0) NULL,
    `hours` INTEGER UNSIGNED NULL,
    `minutes` INTEGER UNSIGNED NULL,

    INDEX `idx_attendance_project_logs_aid`(`attendance_id`),
    INDEX `idx_project_id`(`project_id`),
    INDEX `idx_started_at`(`started_at`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `audit_logs` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `user_id` INTEGER NULL,
    `username` VARCHAR(100) NULL,
    `user_ip` VARCHAR(45) NULL,
    `action` VARCHAR(100) NOT NULL,
    `entity_type` VARCHAR(50) NULL,
    `entity_id` INTEGER NULL,
    `description` TEXT NULL,
    `old_values` LONGTEXT NULL,
    `new_values` LONGTEXT NULL,
    `user_agent` TEXT NULL,
    `session_id` VARCHAR(128) NULL,
    `created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),

    INDEX `idx_audit_log_created`(`created_at`),
    INDEX `idx_audit_logs_action`(`action`),
    INDEX `idx_audit_logs_created`(`created_at`),
    INDEX `idx_audit_logs_entity`(`entity_type`, `entity_id`, `created_at`),
    INDEX `idx_audit_logs_user_created`(`user_id`, `created_at`),
    FULLTEXT INDEX `idx_audit_search`(`description`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `bank_accounts` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `account_name` VARCHAR(255) NULL,
    `bank_name` VARCHAR(255) NULL,
    `account_number` VARCHAR(50) NULL,
    `iban` VARCHAR(50) NULL,
    `bic` VARCHAR(20) NULL,
    `currency` VARCHAR(10) NULL DEFAULT 'CZK',
    `is_default` BOOLEAN NULL DEFAULT false,
    `position` INTEGER NULL DEFAULT 0,
    `created_at` DATETIME(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `modified_at` DATETIME(0) NULL,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `company_settings` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `company_name` VARCHAR(255) NULL,
    `street` VARCHAR(255) NULL,
    `city` VARCHAR(255) NULL,
    `postal_code` VARCHAR(20) NULL,
    `country` VARCHAR(100) NULL,
    `company_id` VARCHAR(50) NULL,
    `vat_id` VARCHAR(50) NULL,
    `custom_fields` LONGTEXT NULL,
    `logo_data` LONGBLOB NULL,
    `logo_data_dark` MEDIUMBLOB NULL,
    `quotation_prefix` VARCHAR(20) NULL,
    `default_currency` VARCHAR(10) NULL DEFAULT 'CZK',
    `default_vat_rate` DECIMAL(5, 2) NULL DEFAULT 21.00,
    `uuid` VARCHAR(36) NULL,
    `modified_at` DATETIME(0) NULL,
    `is_deleted` BOOLEAN NULL DEFAULT false,
    `sync_version` INTEGER NULL DEFAULT 0,
    `order_type_code` VARCHAR(10) NULL,
    `invoice_type_code` VARCHAR(10) NULL,
    `require_2fa` BOOLEAN NOT NULL DEFAULT false,
    `break_threshold_hours` DECIMAL(4, 2) NULL DEFAULT 6.00,
    `break_duration_short` INTEGER NULL DEFAULT 15,
    `break_duration_long` INTEGER NULL DEFAULT 30,
    `clock_rounding_minutes` INTEGER NULL DEFAULT 15,
    `invoice_alert_email` VARCHAR(255) NULL,
    `leave_notify_email` VARCHAR(255) NULL,
    `max_login_attempts` INTEGER NULL DEFAULT 5,
    `lockout_minutes` INTEGER NULL DEFAULT 15,
    `max_requests_per_minute` INTEGER NULL DEFAULT 300,
    `available_vat_rates` LONGTEXT NULL,
    `available_currencies` LONGTEXT NULL,
    `smtp_from` VARCHAR(255) NULL,
    `smtp_from_name` VARCHAR(255) NULL,
    `offer_number_pattern` VARCHAR(100) NULL,
    `order_number_pattern` VARCHAR(100) NULL,
    `invoice_number_pattern` VARCHAR(100) NULL,

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

-- CreateTable
CREATE TABLE `customers` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL,
    `street` VARCHAR(255) NULL,
    `city` VARCHAR(255) NULL,
    `postal_code` VARCHAR(20) NULL,
    `country` VARCHAR(100) NULL,
    `company_id` VARCHAR(50) NULL,
    `vat_id` VARCHAR(50) NULL,
    `custom_fields` LONGTEXT NULL,
    `created_at` DATETIME(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `uuid` VARCHAR(36) NULL,
    `modified_at` DATETIME(0) NULL,
    `sync_version` INTEGER NULL DEFAULT 0,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `invoice_items` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `invoice_id` INTEGER NOT NULL,
    `description` VARCHAR(500) NULL,
    `quantity` DECIMAL(12, 3) NULL DEFAULT 1.000,
    `unit` VARCHAR(20) NULL,
    `unit_price` DECIMAL(12, 2) NULL DEFAULT 0.00,
    `vat_rate` DECIMAL(5, 2) NULL DEFAULT 21.00,
    `position` INTEGER NULL DEFAULT 0,

    INDEX `invoice_id`(`invoice_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `invoices` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `invoice_number` VARCHAR(50) NULL,
    `order_id` INTEGER NULL,
    `customer_id` INTEGER NULL,
    `status` VARCHAR(30) NULL DEFAULT 'issued',
    `currency` VARCHAR(10) NULL DEFAULT 'CZK',
    `vat_rate` DECIMAL(5, 2) NULL DEFAULT 21.00,
    `apply_vat` BOOLEAN NULL DEFAULT true,
    `payment_method` VARCHAR(50) NULL,
    `constant_symbol` VARCHAR(20) NULL,
    `bank_name` VARCHAR(255) NULL,
    `bank_swift` VARCHAR(20) NULL,
    `bank_iban` VARCHAR(50) NULL,
    `bank_account` VARCHAR(50) NULL,
    `issue_date` DATE NULL,
    `due_date` DATE NULL,
    `tax_date` DATE NULL,
    `paid_date` DATE NULL,
    `issued_by` VARCHAR(255) NULL,
    `billing_text` VARCHAR(500) NULL,
    `language` VARCHAR(5) NULL DEFAULT 'cs',
    `notes` TEXT NULL,
    `internal_notes` TEXT NULL,
    `created_at` DATETIME(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `modified_at` DATETIME(0) NULL,

    UNIQUE INDEX `idx_invoices_number_unique`(`invoice_number`),
    INDEX `customer_id`(`customer_id`),
    INDEX `idx_invoices_due_date`(`due_date`),
    INDEX `idx_invoices_status_issue`(`status`, `issue_date`),
    INDEX `idx_invoices_status_due`(`status`, `due_date`),
    INDEX `order_id`(`order_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `item_templates` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(255) NULL,
    `description` TEXT NULL,
    `default_price` DECIMAL(12, 2) NULL DEFAULT 0.00,
    `category` VARCHAR(100) NULL,
    `uuid` VARCHAR(36) NULL,
    `modified_at` DATETIME(0) NULL,
    `is_deleted` BOOLEAN NULL DEFAULT false,
    `sync_version` INTEGER NULL DEFAULT 0,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `leave_balances` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `user_id` INTEGER NOT NULL,
    `year` INTEGER NOT NULL,
    `vacation_total` DECIMAL(5, 2) NULL DEFAULT 160.00,
    `vacation_used` DECIMAL(5, 2) NULL DEFAULT 0.00,
    `sick_used` DECIMAL(5, 2) NULL DEFAULT 0.00,
    `created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),

    UNIQUE INDEX `idx_leave_balances_user_year`(`user_id`, `year`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `leave_requests` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `user_id` INTEGER NOT NULL,
    `leave_type` ENUM('vacation', 'sick', 'unpaid') NOT NULL,
    `date_from` DATE NOT NULL,
    `date_to` DATE NOT NULL,
    `total_hours` DECIMAL(5, 2) NOT NULL,
    `total_days` INTEGER NOT NULL,
    `notes` TEXT NULL,
    `status` ENUM('pending', 'approved', 'rejected', 'cancelled') NULL DEFAULT 'pending',
    `reviewer_id` INTEGER NULL,
    `reviewer_note` TEXT NULL,
    `reviewed_at` DATETIME(0) NULL,
    `created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),

    INDEX `idx_leave_requests_user_status`(`user_id`, `status`),
    INDEX `idx_status`(`status`),
    INDEX `reviewer_id`(`reviewer_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `number_sequences` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `type` VARCHAR(50) NULL,
    `year` INTEGER NULL,
    `last_number` INTEGER NULL DEFAULT 0,

    UNIQUE INDEX `idx_number_sequences_type_year`(`type`, `year`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `order_items` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `order_id` INTEGER NOT NULL,
    `description` VARCHAR(500) NULL,
    `item_description` TEXT NULL,
    `quantity` DECIMAL(12, 3) NULL DEFAULT 1.000,
    `unit` VARCHAR(20) NULL,
    `unit_price` DECIMAL(12, 2) NULL DEFAULT 0.00,
    `is_included_in_total` BOOLEAN NULL DEFAULT true,
    `position` INTEGER NULL DEFAULT 0,
    `modified_at` DATETIME(0) NULL,

    INDEX `order_id`(`order_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `order_sections` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `order_id` INTEGER NOT NULL,
    `title` VARCHAR(500) NULL,
    `title_cz` VARCHAR(500) NULL,
    `content` TEXT NULL,
    `position` INTEGER NULL DEFAULT 0,
    `modified_at` DATETIME(0) NULL,

    INDEX `order_id`(`order_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `orders` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `order_number` VARCHAR(50) NULL,
    `customer_order_number` VARCHAR(100) NULL,
    `attachment_data` LONGBLOB NULL,
    `attachment_name` VARCHAR(255) NULL,
    `quotation_id` INTEGER NULL,
    `customer_id` INTEGER NULL,
    `status` VARCHAR(30) NULL DEFAULT 'prijata',
    `currency` VARCHAR(10) NULL DEFAULT 'CZK',
    `language` VARCHAR(5) NULL DEFAULT 'cs',
    `vat_rate` DECIMAL(5, 2) NULL DEFAULT 21.00,
    `apply_vat` BOOLEAN NULL DEFAULT true,
    `exchange_rate` DECIMAL(10, 4) NULL DEFAULT 1.0000,
    `scope_title` VARCHAR(500) NULL,
    `scope_description` TEXT NULL,
    `notes` TEXT NULL,
    `created_at` DATETIME(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `modified_at` DATETIME(0) NULL,

    UNIQUE INDEX `idx_orders_number_unique`(`order_number`),
    INDEX `customer_id`(`customer_id`),
    INDEX `quotation_id`(`quotation_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `permissions` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(50) NOT NULL,
    `display_name` VARCHAR(100) NOT NULL,
    `description` TEXT NULL,
    `module` VARCHAR(50) NOT NULL,
    `created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),

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

-- CreateTable
CREATE TABLE `project_notes` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `project_id` INTEGER NOT NULL,
    `user_id` INTEGER NULL,
    `user_name` VARCHAR(100) NULL,
    `content` TEXT NULL,
    `created_at` DATETIME(0) NULL DEFAULT CURRENT_TIMESTAMP(0),

    INDEX `project_id`(`project_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `projects` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `project_number` VARCHAR(50) NULL,
    `name` VARCHAR(255) NULL,
    `customer_id` INTEGER NULL,
    `responsible_user_id` INTEGER NULL,
    `quotation_id` INTEGER NULL,
    `order_id` INTEGER NULL,
    `status` VARCHAR(30) NULL DEFAULT 'aktivni',
    `start_date` DATE NULL,
    `end_date` DATE NULL,
    `notes` TEXT NULL,
    `created_at` DATETIME(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `modified_at` DATETIME(0) NULL,

    UNIQUE INDEX `projects_project_number_key`(`project_number`),
    INDEX `customer_id`(`customer_id`),
    INDEX `fk_projects_responsible_user`(`responsible_user_id`),
    INDEX `order_id`(`order_id`),
    INDEX `quotation_id`(`quotation_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `quotation_items` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `quotation_id` INTEGER NOT NULL,
    `position` INTEGER NULL DEFAULT 0,
    `description` VARCHAR(500) NULL,
    `item_description` TEXT NULL,
    `quantity` DECIMAL(12, 3) NULL DEFAULT 1.000,
    `unit` VARCHAR(20) NULL,
    `unit_price` DECIMAL(12, 2) NULL DEFAULT 0.00,
    `is_included_in_total` BOOLEAN NULL DEFAULT true,
    `uuid` VARCHAR(36) NULL,
    `modified_at` DATETIME(0) NULL,
    `is_deleted` BOOLEAN NULL DEFAULT false,
    `sync_version` INTEGER NULL DEFAULT 0,

    INDEX `quotation_id`(`quotation_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `quotations` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `quotation_number` VARCHAR(50) NULL,
    `project_code` VARCHAR(50) NULL,
    `customer_id` INTEGER NULL,
    `created_at` DATETIME(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `valid_until` DATE NULL,
    `currency` VARCHAR(10) NULL DEFAULT 'CZK',
    `language` VARCHAR(5) NULL DEFAULT 'cs',
    `vat_rate` DECIMAL(5, 2) NULL DEFAULT 21.00,
    `apply_vat` BOOLEAN NULL DEFAULT true,
    `exchange_rate` DECIMAL(10, 4) NULL DEFAULT 1.0000,
    `exchange_rate_date` DATE NULL,
    `order_id` INTEGER NULL,
    `status` VARCHAR(20) NOT NULL DEFAULT 'active',
    `scope_title` VARCHAR(500) NULL,
    `scope_description` TEXT NULL,
    `locked_by` INTEGER NULL,
    `locked_at` DATETIME(0) NULL,
    `uuid` VARCHAR(36) NULL,
    `modified_at` DATETIME(0) NULL,
    `sync_version` INTEGER NULL DEFAULT 0,

    UNIQUE INDEX `quotations_quotation_number_key`(`quotation_number`),
    INDEX `customer_id`(`customer_id`),
    INDEX `idx_quotations_number`(`quotation_number`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `received_invoices` (
    `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
    `month` TINYINT UNSIGNED NOT NULL,
    `year` SMALLINT UNSIGNED NOT NULL,
    `supplier_name` VARCHAR(255) NOT NULL,
    `invoice_number` VARCHAR(100) NULL,
    `description` VARCHAR(500) NULL,
    `amount` DECIMAL(12, 2) NOT NULL DEFAULT 0.00,
    `currency` VARCHAR(3) NOT NULL DEFAULT 'CZK',
    `vat_rate` DECIMAL(5, 2) NOT NULL DEFAULT 21.00,
    `vat_amount` DECIMAL(12, 2) NOT NULL DEFAULT 0.00,
    `issue_date` DATE NULL,
    `due_date` DATE NULL,
    `paid_date` DATE NULL,
    `status` ENUM('unpaid', 'paid') NOT NULL DEFAULT 'unpaid',
    `file_name` VARCHAR(255) NULL,
    `file_mime` VARCHAR(100) NULL,
    `file_size` INTEGER UNSIGNED NULL,
    `notes` TEXT NULL,
    `uploaded_by` INTEGER UNSIGNED NULL,
    `created_at` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
    `modified_at` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),

    INDEX `idx_month_year`(`year`, `month`),
    INDEX `idx_status`(`status`),
    INDEX `idx_supplier`(`supplier_name`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `refresh_tokens` (
    `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
    `user_id` INTEGER UNSIGNED NOT NULL,
    `token_hash` VARCHAR(64) NOT NULL,
    `expires_at` DATETIME(0) NOT NULL,
    `replaced_at` DATETIME(0) NULL,
    `replaced_by_hash` VARCHAR(64) NULL,
    `remember_me` BOOLEAN NOT NULL DEFAULT false,
    `ip_address` VARCHAR(45) NULL,
    `user_agent` VARCHAR(255) NULL,
    `created_at` DATETIME(0) NULL DEFAULT CURRENT_TIMESTAMP(0),

    UNIQUE INDEX `token_hash`(`token_hash`),
    INDEX `idx_refresh_tokens_expires_at`(`expires_at`),
    INDEX `idx_refresh_tokens_user_exp`(`user_id`, `expires_at`),
    INDEX `idx_refresh_tokens_user_id`(`user_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `role_permissions` (
    `role_id` INTEGER NOT NULL,
    `permission_id` INTEGER NOT NULL,

    INDEX `permission_id`(`permission_id`),
    PRIMARY KEY (`role_id`, `permission_id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `roles` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(50) NOT NULL,
    `display_name` VARCHAR(100) NOT NULL,
    `description` TEXT NULL,
    `created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),

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

-- CreateTable
CREATE TABLE `scope_sections` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `quotation_id` INTEGER NOT NULL,
    `position` INTEGER NULL DEFAULT 0,
    `title` VARCHAR(500) NULL,
    `title_cz` VARCHAR(500) NULL,
    `content` TEXT NULL,
    `content_editor_height` INTEGER NULL,
    `uuid` VARCHAR(36) NULL,
    `modified_at` DATETIME(0) NULL,
    `is_deleted` BOOLEAN NULL DEFAULT false,
    `sync_version` INTEGER NULL DEFAULT 0,

    INDEX `quotation_id`(`quotation_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `scope_template_sections` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `scope_template_id` INTEGER NOT NULL,
    `position` INTEGER NULL DEFAULT 0,
    `title` VARCHAR(500) NULL,
    `title_cz` VARCHAR(500) NULL,
    `content` TEXT NULL,
    `uuid` VARCHAR(36) NULL,
    `modified_at` DATETIME(0) NULL,
    `is_deleted` BOOLEAN NULL DEFAULT false,
    `sync_version` INTEGER NULL DEFAULT 0,

    INDEX `scope_template_id`(`scope_template_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `scope_templates` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(255) NULL,
    `title` VARCHAR(500) NULL,
    `description` TEXT NULL,
    `uuid` VARCHAR(36) NULL,
    `modified_at` DATETIME(0) NULL,
    `is_deleted` BOOLEAN NULL DEFAULT false,
    `sync_version` INTEGER NULL DEFAULT 0,

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `totp_login_tokens` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `user_id` INTEGER NOT NULL,
    `token_hash` VARCHAR(64) NOT NULL,
    `expires_at` DATETIME(0) NOT NULL,
    `created_at` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),

    INDEX `idx_expires`(`expires_at`),
    INDEX `idx_totp_login_tokens_hash`(`token_hash`),
    INDEX `idx_user_id`(`user_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `trips` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `vehicle_id` INTEGER NOT NULL,
    `user_id` INTEGER NOT NULL,
    `trip_date` DATE NOT NULL,
    `start_km` INTEGER NOT NULL,
    `end_km` INTEGER NOT NULL,
    `distance` INTEGER NULL,
    `route_from` VARCHAR(100) NOT NULL,
    `route_to` VARCHAR(100) NOT NULL,
    `is_business` BOOLEAN NOT NULL DEFAULT true,
    `notes` TEXT NULL,
    `created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),

    INDEX `trip_date`(`trip_date`),
    INDEX `user_id`(`user_id`),
    INDEX `vehicle_id`(`vehicle_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `users` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(50) NOT NULL,
    `email` VARCHAR(255) NOT NULL,
    `password_hash` VARCHAR(255) NOT NULL,
    `first_name` VARCHAR(50) NOT NULL,
    `last_name` VARCHAR(50) NOT NULL,
    `role_id` INTEGER NULL,
    `is_active` BOOLEAN NULL DEFAULT true,
    `last_login` TIMESTAMP(0) NULL,
    `failed_login_attempts` INTEGER NULL DEFAULT 0,
    `locked_until` TIMESTAMP(0) NULL,
    `password_changed_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `totp_secret` VARCHAR(255) NULL,
    `totp_enabled` BOOLEAN NOT NULL DEFAULT false,
    `totp_backup_codes` TEXT NULL,
    `totp_last_used_counter` INTEGER NULL,

    UNIQUE INDEX `username`(`username`),
    UNIQUE INDEX `email`(`email`),
    INDEX `idx_users_is_active`(`is_active`),
    INDEX `idx_users_role_id`(`role_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `vehicles` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `spz` VARCHAR(20) NOT NULL,
    `name` VARCHAR(100) NOT NULL,
    `brand` VARCHAR(50) NULL,
    `model` VARCHAR(50) NULL,
    `initial_km` INTEGER NOT NULL DEFAULT 0,
    `actual_km` INTEGER NOT NULL DEFAULT 0,
    `is_active` BOOLEAN NOT NULL DEFAULT true,
    `created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
    `updated_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),

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

-- CreateTable
CREATE TABLE `invoice_alert_log` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `invoice_type` VARCHAR(20) NOT NULL,
    `invoice_id` INTEGER NOT NULL,
    `alert_type` VARCHAR(20) NOT NULL,
    `sent_at` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
    `created_at` DATETIME(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),

    UNIQUE INDEX `invoice_type_invoice_id_alert_type`(`invoice_type`, `invoice_id`, `alert_type`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `attendance` ADD CONSTRAINT `attendance_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `attendance_project_logs` ADD CONSTRAINT `attendance_project_logs_attendance_id_fkey` FOREIGN KEY (`attendance_id`) REFERENCES `attendance`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `attendance_project_logs` ADD CONSTRAINT `attendance_project_logs_project_id_fkey` FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `invoice_items` ADD CONSTRAINT `invoice_items_ibfk_1` FOREIGN KEY (`invoice_id`) REFERENCES `invoices`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `invoices` ADD CONSTRAINT `invoices_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `invoices` ADD CONSTRAINT `invoices_ibfk_2` FOREIGN KEY (`customer_id`) REFERENCES `customers`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `leave_balances` ADD CONSTRAINT `leave_balances_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `leave_requests` ADD CONSTRAINT `leave_requests_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `leave_requests` ADD CONSTRAINT `leave_requests_ibfk_2` FOREIGN KEY (`reviewer_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `order_items` ADD CONSTRAINT `order_items_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `order_sections` ADD CONSTRAINT `order_sections_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `orders` ADD CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`quotation_id`) REFERENCES `quotations`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `orders` ADD CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`customer_id`) REFERENCES `customers`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `project_notes` ADD CONSTRAINT `project_notes_ibfk_1` FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `projects` ADD CONSTRAINT `fk_projects_responsible_user` FOREIGN KEY (`responsible_user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `projects` ADD CONSTRAINT `projects_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customers`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `projects` ADD CONSTRAINT `projects_ibfk_2` FOREIGN KEY (`quotation_id`) REFERENCES `quotations`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `projects` ADD CONSTRAINT `projects_ibfk_3` FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `quotation_items` ADD CONSTRAINT `quotation_items_ibfk_1` FOREIGN KEY (`quotation_id`) REFERENCES `quotations`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `quotations` ADD CONSTRAINT `quotations_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customers`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `role_permissions` ADD CONSTRAINT `role_permissions_ibfk_1` FOREIGN KEY (`role_id`) REFERENCES `roles`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `role_permissions` ADD CONSTRAINT `role_permissions_ibfk_2` FOREIGN KEY (`permission_id`) REFERENCES `permissions`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `scope_sections` ADD CONSTRAINT `scope_sections_ibfk_1` FOREIGN KEY (`quotation_id`) REFERENCES `quotations`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `scope_template_sections` ADD CONSTRAINT `scope_template_sections_ibfk_1` FOREIGN KEY (`scope_template_id`) REFERENCES `scope_templates`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `trips` ADD CONSTRAINT `trips_user_fk` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `trips` ADD CONSTRAINT `trips_vehicle_fk` FOREIGN KEY (`vehicle_id`) REFERENCES `vehicles`(`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

-- AddForeignKey
ALTER TABLE `users` ADD CONSTRAINT `users_ibfk_1` FOREIGN KEY (`role_id`) REFERENCES `roles`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;
