-- 014_create_payments_table.sql
CREATE TABLE IF NOT EXISTS `payments` (
    `id`                BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    `hoa_don_id`        BIGINT UNSIGNED NOT NULL,
    `so_tien`           DECIMAL(15,2) NOT NULL DEFAULT 0.00,
    `ngay_thanh_toan`   DATE NOT NULL,
    `phuong_thuc`       ENUM('tien_mat','chuyen_khoan','momo','zalopay','vnpay','khac') NOT NULL DEFAULT 'tien_mat',
    `ngan_hang`         VARCHAR(100) DEFAULT NULL,
    `ma_giao_dich`      VARCHAR(100) DEFAULT NULL,
    `ghi_chu`           TEXT DEFAULT NULL,
    `nguoi_nhan_id`     BIGINT UNSIGNED NOT NULL,
    `created_at`        TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at`        TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `payments_hoa_don_id_index` (`hoa_don_id`),
    KEY `payments_nguoi_nhan_id_index` (`nguoi_nhan_id`),
    KEY `payments_ngay_thanh_toan_index` (`ngay_thanh_toan`),
    CONSTRAINT `fk_payments_hoa_don`
        FOREIGN KEY (`hoa_don_id`) REFERENCES `invoices` (`id`) ON DELETE RESTRICT,
    CONSTRAINT `fk_payments_nguoi_nhan`
        FOREIGN KEY (`nguoi_nhan_id`) REFERENCES `users` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
