-- Tabel Pelanggan
CREATE TABLE customers (
    CustomerID INT PRIMARY KEY AUTO_INCREMENT,
    CustomerCode VARCHAR(20) UNIQUE,
    CustomerCompanyName VARCHAR(100),
    CustomerType ENUM('internal', 'external'),
    CustomerAddress TEXT,
    CustomerCity VARCHAR(50),
    CustomerPostalCode VARCHAR(10),
    CustomerPhone VARCHAR(20),
    CustomerEmail VARCHAR(100),
    CustomerPICName VARCHAR(100),
    CustomerPICPhone VARCHAR(20),
    CustomerPICEmail VARCHAR(100),
    CustomerRegistrationDate DATETIME DEFAULT CURRENT_TIMESTAMP,
    CustomerStatus ENUM('active', 'inactive') DEFAULT 'active',
    CustomerCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    CustomerCreatedUserID INT,
    CustomerUpdatedAt DATETIME,
    CustomerUpdatedUserID INT,
    CustomerDeletedAt DATETIME,
    CustomerDeletedUserID INT
);

-- Tabel Riwayat Permintaan Analisis
CREATE TABLE analysis_requests (
    AnalysisRequestID INT PRIMARY KEY AUTO_INCREMENT,
    AnalysisRequestCode VARCHAR(20) UNIQUE,
    CustomerID INT,
    AnalysisRequestDate DATETIME,
    AnalysisRequestStatus ENUM('new', 'process', 'completed', 'cancelled'),
    AnalysisRequestTotalAmount DECIMAL(10,2),
    AnalysisRequestNotes TEXT,
    AnalysisRequestCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    AnalysisRequestCreatedUserID INT,
    AnalysisRequestUpdatedAt DATETIME,
    AnalysisRequestUpdatedUserID INT,
    AnalysisRequestDeletedAt DATETIME,
    AnalysisRequestDeletedUserID INT
);

-- Tabel Riwayat Pembayaran
CREATE TABLE payments (
    PaymentID INT PRIMARY KEY AUTO_INCREMENT,
    AnalysisRequestID INT,
    PaymentCode VARCHAR(20) UNIQUE,
    PaymentDate DATETIME,
    PaymentAmount DECIMAL(10,2),
    PaymentMethod ENUM('cash', 'transfer', 'credit_card'),
    PaymentStatus ENUM('pending', 'completed', 'failed'),
    PaymentProofPath VARCHAR(255),
    PaymentNotes TEXT,
    PaymentCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    PaymentCreatedUserID INT,
    PaymentUpdatedAt DATETIME,
    PaymentUpdatedUserID INT,
    PaymentDeletedAt DATETIME,
    PaymentDeletedUserID INT
);

-- Tabel Kategori Pelanggan
CREATE TABLE customer_categories (
    CustomerCategoryID INT PRIMARY KEY AUTO_INCREMENT,
    CustomerCategoryCode VARCHAR(20) UNIQUE,
    CustomerCategoryName VARCHAR(100),
    CustomerCategoryDescription TEXT,
    CustomerCategoryIsActive BOOLEAN DEFAULT TRUE,
    CustomerCategoryCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    CustomerCategoryCreatedUserID INT,
    CustomerCategoryUpdatedAt DATETIME,
    CustomerCategoryUpdatedUserID INT,
    CustomerCategoryDeletedAt DATETIME,
    CustomerCategoryDeletedUserID INT
);

-- Tabel Kontak Tambahan Pelanggan
CREATE TABLE customer_contacts (
    CustomerContactID INT PRIMARY KEY AUTO_INCREMENT,
    CustomerID INT,
    CustomerContactName VARCHAR(100),
    CustomerContactPosition VARCHAR(100),
    CustomerContactDepartment VARCHAR(100),
    CustomerContactPhone VARCHAR(20),
    CustomerContactEmail VARCHAR(100),
    CustomerContactIsMainContact BOOLEAN DEFAULT FALSE,
    CustomerContactIsActive BOOLEAN DEFAULT TRUE,
    CustomerContactCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    CustomerContactCreatedUserID INT,
    CustomerContactUpdatedAt DATETIME,
    CustomerContactUpdatedUserID INT,
    CustomerContactDeletedAt DATETIME,
    CustomerContactDeletedUserID INT
);

-- Tabel Dokumen Pelanggan
CREATE TABLE customer_documents (
    CustomerDocumentID INT PRIMARY KEY AUTO_INCREMENT,
    CustomerID INT,
    CustomerDocumentType VARCHAR(50),
    CustomerDocumentNumber VARCHAR(100),
    CustomerDocumentName VARCHAR(255),
    CustomerDocumentPath VARCHAR(255),
    CustomerDocumentIssuedDate DATE,
    CustomerDocumentExpiredDate DATE,
    CustomerDocumentIsActive BOOLEAN DEFAULT TRUE,
    CustomerDocumentCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    CustomerDocumentCreatedUserID INT,
    CustomerDocumentUpdatedAt DATETIME,
    CustomerDocumentUpdatedUserID INT,
    CustomerDocumentDeletedAt DATETIME,
    CustomerDocumentDeletedUserID INT
);

-- Tabel Riwayat Aktivitas Pelanggan
CREATE TABLE customer_activities (
    CustomerActivityID INT PRIMARY KEY AUTO_INCREMENT,
    CustomerID INT,
    CustomerActivityType VARCHAR(50),
    CustomerActivityDescription TEXT,
    CustomerActivityDate DATETIME,
    CustomerActivityStatus VARCHAR(50),
    CustomerActivityNotes TEXT,
    CustomerActivityCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    CustomerActivityCreatedUserID INT,
    CustomerActivityUpdatedAt DATETIME,
    CustomerActivityUpdatedUserID INT,
    CustomerActivityDeletedAt DATETIME,
    CustomerActivityDeletedUserID INT
);

-- Tabel Riwayat Komunikasi Pelanggan
CREATE TABLE customer_communications (
    CustomerCommunicationID INT PRIMARY KEY AUTO_INCREMENT,
    CustomerID INT,
    CustomerContactID INT,
    CustomerCommunicationType VARCHAR(50),
    CustomerCommunicationSubject VARCHAR(255),
    CustomerCommunicationContent TEXT,
    CustomerCommunicationDate DATETIME,
    CustomerCommunicationStatus VARCHAR(50),
    CustomerCommunicationFollowUpDate DATETIME,
    CustomerCommunicationNotes TEXT,
    CustomerCommunicationCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    CustomerCommunicationCreatedUserID INT,
    CustomerCommunicationUpdatedAt DATETIME,
    CustomerCommunicationUpdatedUserID INT,
    CustomerCommunicationDeletedAt DATETIME,
    CustomerCommunicationDeletedUserID INT
);

-- Contoh data pelanggan
INSERT INTO customers (
    CustomerCode, 
    CustomerCompanyName, 
    CustomerType, 
    CustomerAddress, 
    CustomerCity, 
    CustomerPostalCode, 
    CustomerPhone, 
    CustomerEmail, 
    CustomerPICName, 
    CustomerPICPhone, 
    CustomerPICEmail
) VALUES
('CUST001', 'PT Lingkungan Hijau', 'external', 'Jl. Raya Utama No. 123', 'Jakarta', '12345', '021-5551234', 'info@lingkunganhijau.com', 'Budi Santoso', '081234567890', 'budi@lingkunganhijau.com'),
('CUST002', 'Dinas Lingkungan Hidup', 'internal', 'Jl. Pemda No. 45', 'Bandung', '40111', '022-7891234', 'dlh@bandung.go.id', 'Siti Aminah', '087812345678', 'siti@bandung.go.id'),
('CUST003', 'CV Alam Lestari', 'external', 'Jl. Industri Blok A2', 'Surabaya', '60111', '031-8765432', 'contact@alamlestari.com', 'Rudi Hartono', '085678901234', 'rudi@alamlestari.com');

-- Contoh data permintaan analisis
INSERT INTO analysis_requests (
    AnalysisRequestCode, 
    CustomerID, 
    AnalysisRequestDate, 
    AnalysisRequestStatus, 
    AnalysisRequestTotalAmount, 
    AnalysisRequestNotes
) VALUES
('REQ001', 1, '2024-03-15 09:00:00', 'completed', 2500000, 'Analisis air limbah'),
('REQ002', 1, '2024-03-16 10:30:00', 'process', 1750000, 'Analisis kualitas udara'),
('REQ003', 2, '2024-03-17 13:15:00', 'new', 3000000, 'Analisis tanah');

-- Contoh data pembayaran
INSERT INTO payments (
    AnalysisRequestID, 
    PaymentCode, 
    PaymentDate, 
    PaymentAmount, 
    PaymentMethod, 
    PaymentStatus, 
    PaymentNotes
) VALUES
(1, 'PAY001', '2024-03-15 14:30:00', 2500000, 'transfer', 'completed', 'Pembayaran lunas'),
(2, 'PAY002', '2024-03-16 11:00:00', 875000, 'transfer', 'completed', 'Pembayaran DP 50%'),
(3, 'PAY003', '2024-03-17 14:00:00', 1500000, 'transfer', 'pending', 'Menunggu konfirmasi transfer');

-- Contoh data kategori pelanggan
INSERT INTO customer_categories (
    CustomerCategoryCode,
    CustomerCategoryName,
    CustomerCategoryDescription
) VALUES
('CAT001', 'Industri', 'Pelanggan dari sektor industri manufaktur'),
('CAT002', 'Pemerintahan', 'Instansi pemerintah dan BUMN'),
('CAT003', 'Komersial', 'Pelanggan komersial non-industri');

-- Contoh data kontak tambahan
INSERT INTO customer_contacts (
    CustomerID,
    CustomerContactName,
    CustomerContactPosition,
    CustomerContactDepartment,
    CustomerContactPhone,
    CustomerContactEmail,
    CustomerContactIsMainContact
) VALUES
(1, 'Andi Wijaya', 'HSE Manager', 'HSE', '081234567891', 'andi@lingkunganhijau.com', TRUE),
(1, 'Diana Putri', 'Lab Coordinator', 'Quality Control', '081234567892', 'diana@lingkunganhijau.com', FALSE),
(2, 'Bambang Kusuma', 'Kepala Seksi', 'Pengawasan', '087812345679', 'bambang@bandung.go.id', TRUE);

-- Contoh data dokumen
INSERT INTO customer_documents (
    CustomerID,
    CustomerDocumentType,
    CustomerDocumentNumber,
    CustomerDocumentName,
    CustomerDocumentPath,
    CustomerDocumentIssuedDate,
    CustomerDocumentExpiredDate
) VALUES
(1, 'SIUP', '123/SIUP/2024', 'SIUP PT Lingkungan Hijau', '/documents/siup/123.pdf', '2024-01-01', '2029-01-01'),
(1, 'NPWP', '01.234.567.8-123.000', 'NPWP PT Lingkungan Hijau', '/documents/npwp/123.pdf', '2024-01-01', NULL),
(2, 'SK', 'SK-123/DLH/2024', 'SK Penunjukan', '/documents/sk/123.pdf', '2024-01-01', '2024-12-31');

-- Contoh data aktivitas
INSERT INTO customer_activities (
    CustomerID,
    CustomerActivityType,
    CustomerActivityDescription,
    CustomerActivityDate,
    CustomerActivityStatus
) VALUES
(1, 'Visit', 'Kunjungan teknis untuk sampling rutin', '2024-03-15 10:00:00', 'Completed'),
(1, 'Meeting', 'Pembahasan hasil analisis', '2024-03-16 13:00:00', 'Scheduled'),
(2, 'Training', 'Pelatihan pengambilan sampel', '2024-03-17 09:00:00', 'Scheduled');

-- Contoh data komunikasi
INSERT INTO customer_communications (
    CustomerID,
    CustomerContactID,
    CustomerCommunicationType,
    CustomerCommunicationSubject,
    CustomerCommunicationContent,
    CustomerCommunicationDate,
    CustomerCommunicationStatus
) VALUES
(1, 1, 'Email', 'Konfirmasi Jadwal Sampling', 'Konfirmasi jadwal sampling minggu depan', '2024-03-14 09:00:00', 'Sent'),
(1, 2, 'Phone', 'Diskusi Hasil Analisis', 'Diskusi tentang hasil analisis yang melebihi baku mutu', '2024-03-15 14:00:00', 'Completed'),
(2, 3, 'Meeting', 'Rapat Koordinasi', 'Rapat koordinasi program pemantauan lingkungan', '2024-03-16 10:00:00', 'Scheduled');