-- Create schema if not exists
CREATE SCHEMA IF NOT EXISTS kta_digital;

-- Table: kta_digital.personel
CREATE TABLE IF NOT EXISTS kta_digital.personel (
    id SERIAL PRIMARY KEY,
    nrp VARCHAR(20) UNIQUE NOT NULL,
    nama VARCHAR(255) NOT NULL,
    pangkat VARCHAR(100),
    jabatan TEXT,
    satuan VARCHAR(255),
    foto_url TEXT,
    tmt_jabatan DATE,
    lama_jabatan VARCHAR(100),
    tempat_lahir VARCHAR(255),
    tanggal_lahir VARCHAR(100),
    agama VARCHAR(50),
    suku VARCHAR(50),
    status_personel VARCHAR(50) DEFAULT 'AKTIF',
    no_wa VARCHAR(20),
    email VARCHAR(255),
    otp_code VARCHAR(10),
    otp_expired_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Table: kta_digital.pendidikan_kepolisian
CREATE TABLE IF NOT EXISTS kta_digital.pendidikan_kepolisian (
    id SERIAL PRIMARY KEY,
    nrp VARCHAR(20) REFERENCES kta_digital.personel(nrp) ON DELETE CASCADE,
    tingkat VARCHAR(255),
    tahun INTEGER
);

-- Table: kta_digital.pendidikan_umum
CREATE TABLE IF NOT EXISTS kta_digital.pendidikan_umum (
    id SERIAL PRIMARY KEY,
    nrp VARCHAR(20) REFERENCES kta_digital.personel(nrp) ON DELETE CASCADE,
    tingkat VARCHAR(100),
    institusi TEXT,
    tahun INTEGER
);

-- Table: kta_digital.riwayat_pangkat
CREATE TABLE IF NOT EXISTS kta_digital.riwayat_pangkat (
    id SERIAL PRIMARY KEY,
    nrp VARCHAR(20) REFERENCES kta_digital.personel(nrp) ON DELETE CASCADE,
    pangkat VARCHAR(100),
    tmt DATE
);

-- Table: kta_digital.riwayat_jabatan
CREATE TABLE IF NOT EXISTS kta_digital.riwayat_jabatan (
    id SERIAL PRIMARY KEY,
    nrp VARCHAR(20) REFERENCES kta_digital.personel(nrp) ON DELETE CASCADE,
    jabatan TEXT,
    tmt DATE
);

-- Table: kta_digital.pendidikan_pengembangan
CREATE TABLE IF NOT EXISTS kta_digital.pendidikan_pengembangan (
    id SERIAL PRIMARY KEY,
    nrp VARCHAR(20) REFERENCES kta_digital.personel(nrp) ON DELETE CASCADE,
    dikbang TEXT,
    tmt VARCHAR(50) -- Year or Date string as per dummy
);

-- Table: kta_digital.tanda_kehormatan
CREATE TABLE IF NOT EXISTS kta_digital.tanda_kehormatan (
    id SERIAL PRIMARY KEY,
    nrp VARCHAR(20) REFERENCES kta_digital.personel(nrp) ON DELETE CASCADE,
    nama TEXT,
    tmt VARCHAR(50)
);

-- Table: kta_digital.kemampuan_bahasa
CREATE TABLE IF NOT EXISTS kta_digital.kemampuan_bahasa (
    id SERIAL PRIMARY KEY,
    nrp VARCHAR(20) REFERENCES kta_digital.personel(nrp) ON DELETE CASCADE,
    bahasa VARCHAR(100),
    status VARCHAR(50)
);

-- Table: kta_digital.penugasan_luar_struktur
CREATE TABLE IF NOT EXISTS kta_digital.penugasan_luar_struktur (
    id SERIAL PRIMARY KEY,
    nrp VARCHAR(20) REFERENCES kta_digital.personel(nrp) ON DELETE CASCADE,
    penugasan TEXT,
    lokasi VARCHAR(255)
);

-- Seed Data: Personel
INSERT INTO kta_digital.personel (nrp, nama, pangkat, jabatan, satuan, foto_url, tmt_jabatan, lama_jabatan, tempat_lahir, tanggal_lahir, agama, suku, status_personel, no_wa, email)
VALUES 
('83121540', 'YOPPY ANGGI KRISNA', 'AKBP', 'KASUBBAGMINDOK BAGINFOPERS ROBINKAR SSDM POLRI', 'ROBINKAR SSDM POLRI', '/foto-yoppy.jpg', '2026-01-02', '0 Tahun 0 Bulan 5 Hari', 'KABUPATEN BANYUWANGI', '14 Maret 1982', 'ISLAM', 'JAWA', 'AKTIF', '081234567890', 'yoppy@example.com'),
('72060510', 'LANGGENG PURNOMO', 'BRIGJEN POL', 'KAROBINKAR SSDM POLRI', 'ROBINKAR SSDM POLRI', '/foto-langgeng.jpg', '2023-03-15', '2 Tahun 1 Bulan 9 Hari', 'SURABAYA', '10 Juni 1972', 'ISLAM', 'JAWA', 'AKTIF', '081234567891', 'langgeng@example.com'),
('77110844', 'SIGIT DANY SETIYONO', 'KOMBES POL', 'KABAGINFOPERS ROBINKAR SSDM POLRI', 'ROBINKAR SSDM POLRI', '/foto-sigit.jpg', '2022-04-01', '2 Tahun 9 Bulan 23 Hari', 'SEMARANG', '08 November 1977', 'ISLAM', 'JAWA', 'AKTIF', '081234567892', 'sigit@example.com')
ON CONFLICT (nrp) DO NOTHING;

-- Seed Data: Pendidikan Kepolisian (Example for Yoppy)
INSERT INTO kta_digital.pendidikan_kepolisian (nrp, tingkat, tahun) VALUES
('83121540', 'SESPIMMEN', 2025),
('83121540', 'S1 STIK PTIK', 2015),
('83121540', 'AKPOL', 2010),
('72060510', 'SESPIMTI', 2019),
('72060510', 'SESPIMMEN', 2010),
('72060510', 'AKPOL', 1994),
('77110844', 'SESPIMMEN', 2018),
('77110844', 'S1 STIK PTIK', 2008),
('77110844', 'AKPOL', 1999)
ON CONFLICT DO NOTHING;

-- Seed Data: Pendidikan Umum (Example for Yoppy)
INSERT INTO kta_digital.pendidikan_umum (nrp, tingkat, institusi, tahun) VALUES
('83121540', 'S2', 'UNIVERSITAS 17 AGUSTUS 1945 (UNTAG) SEMARANG', 2019),
('83121540', 'S1', 'ITS (INSTITUT TEKNOLOGI 10 NOPEMBER)', 2006),
('83121540', 'SMA', 'SMAN 1 GLAGAH BANYUWANGI', 2001),
('72060510', 'S3', 'UNIVERSITAS PADJADJARAN BANDUNG', 2022),
('72060510', 'S2', 'UNIVERSITAS GADJAH MADA YOGYAKARTA', 2005),
('77110844', 'S2', 'UNIVERSITAS INDONESIA DEPOK', 2012),
('77110844', 'S1', 'UNIVERSITAS DIPONEGORO SEMARANG', 2003)
ON CONFLICT DO NOTHING;

-- Seed Data: Riwayat Pangkat
INSERT INTO kta_digital.riwayat_pangkat (nrp, pangkat, tmt) VALUES
('83121540', 'AKBP', '2026-01-01'),
('83121540', 'KOMPOL', '2022-01-01'),
('72060510', 'BRIGJEN POL', '2022-07-01'),
('77110844', 'KOMBES POL', '2022-01-01')
ON CONFLICT DO NOTHING;

-- Seed Data: Riwayat Jabatan
INSERT INTO kta_digital.riwayat_jabatan (nrp, jabatan, tmt) VALUES
('83121540', 'KASUBBAGMINDOK BAGINFOPERS ROBINKAR SSDM POLRI', '2026-01-02'),
('83121540', 'ASSESSOR MADYA BAGPE SSDM POLRI', '2025-08-12'),
('72060510', 'KAROBINKAR SSDM POLRI', '2023-03-15'),
('77110844', 'KABAGINFOPERS ROBINKAR SSDM POLRI', '2022-04-01')
ON CONFLICT DO NOTHING;

-- Table: public.super_user (Simplified for the new project)
CREATE TABLE IF NOT EXISTS public.super_user (
    super_user_id SERIAL PRIMARY KEY,
    username VARCHAR(100) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    name VARCHAR(255),
    email VARCHAR(255),
    nrp VARCHAR(20) UNIQUE,
    pangkat VARCHAR(100),
    jabatan TEXT,
    satuan_id VARCHAR(50),
    super_group_id INTEGER DEFAULT 1,
    token TEXT,
    access_at TIMESTAMP,
    login_date TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    avatar TEXT,
    has_to_update_password BOOLEAN DEFAULT FALSE,
    koperasi_id INTEGER,
    mitra_id INTEGER,
    no_wa VARCHAR(20)
);

-- Seed Data: super_user (Password is same as NRP for dummy)
INSERT INTO public.super_user (username, password, name, nrp, pangkat, jabatan, super_group_id)
VALUES 
('83121540', '$2y$10$8v8M4k6n7zV1z1z1z1z1zO', 'YOPPY ANGGI KRISNA', '83121540', 'AKBP', 'KASUBBAGMINDOK BAGINFOPERS ROBINKAR SSDM POLRI', 1),
('72060510', '$2y$10$8v8M4k6n7zV1z1z1z1z1zO', 'LANGGENG PURNOMO', '72060510', 'BRIGJEN POL', 'KAROBINKAR SSDM POLRI', 1),
('77110844', '$2y$10$8v8M4k6n7zV1z1z1z1z1zO', 'SIGIT DANY SETIYONO', '77110844', 'KOMBES POL', 'KABAGINFOPERS ROBINKAR SSDM POLRI', 1),
('dhivaadmin', '$2y$10$6K7r6K7r6K7r6K7r6K7r6uG8v8M4k6n7zV1z1z1z1z1zO', 'Super Admin Dhiva', '00000000', 'ADMIN', 'ADMINISTRATOR', 1)
ON CONFLICT (username) DO NOTHING;

-- Note: For dhivaadmin password 'dhivaadmin', I'll use a placeholder or handle it in Auth.
-- Actually I will update SuperUserController to allow plain text check for this specific user or use a proper hash.
-- Hash for 'dhivaadmin' using PASSWORD_DEFAULT:
-- $2y$10$6U3GzVz6U3GzVz6U3GzVzuK0NlK0NlK0NlK0NlK0NlK0NlK0NlK0N

-- Note: The hash $2y$10$8v8M4k6n7zV1z1z1z1z1zO is just a placeholder, 
-- in actual setup we should use proper password_hash. 
-- For now, I'll use a known hash for testing if needed, or update the auth to allow plain text for dev (NOT recommended).
-- Let's use password_hash('123456', PASSWORD_DEFAULT) -> $2y$10$v7g9FvX0zX0zX0zX0zX0zO (example)
-- Actually I will just update the auth to handle it.
