-- GUARDiA HRM v1.0 — PostgreSQL 스키마
-- DROP TABLE 없이 CREATE TABLE IF NOT EXISTS 사용

-- 사용자/권한
CREATE TABLE IF NOT EXISTS hrm_users (
    id            BIGSERIAL PRIMARY KEY,
    username      VARCHAR(50) NOT NULL UNIQUE,
    password_hash VARCHAR(255) NOT NULL,
    display_name  VARCHAR(100),
    email         VARCHAR(200),
    role          VARCHAR(30) NOT NULL DEFAULT 'HR_STAFF',
    active        BOOLEAN NOT NULL DEFAULT true,
    created_at    TIMESTAMP NOT NULL DEFAULT NOW()
);

-- 역할: SUPERADMIN / MANAGER / HR_STAFF / VIEWER

-- 부서
CREATE TABLE IF NOT EXISTS hrm_departments (
    id             BIGSERIAL PRIMARY KEY,
    dept_code      VARCHAR(30) NOT NULL UNIQUE,
    dept_name      VARCHAR(100) NOT NULL,
    parent_id      BIGINT REFERENCES hrm_departments(id),
    manager_emp_id BIGINT,
    sort_order     INT NOT NULL DEFAULT 0,
    active         BOOLEAN NOT NULL DEFAULT true,
    created_at     TIMESTAMP NOT NULL DEFAULT NOW()
);

-- 직책
CREATE TABLE IF NOT EXISTS hrm_positions (
    id            BIGSERIAL PRIMARY KEY,
    position_code VARCHAR(30) NOT NULL UNIQUE,
    position_name VARCHAR(100) NOT NULL,
    sort_order    INT NOT NULL DEFAULT 0,
    active        BOOLEAN NOT NULL DEFAULT true
);

-- 직급
CREATE TABLE IF NOT EXISTS hrm_grades (
    id          BIGSERIAL PRIMARY KEY,
    grade_code  VARCHAR(30) NOT NULL UNIQUE,
    grade_name  VARCHAR(100) NOT NULL,
    grade_level INT NOT NULL DEFAULT 1,
    sort_order  INT NOT NULL DEFAULT 0,
    active      BOOLEAN NOT NULL DEFAULT true
);

-- 사원
CREATE TABLE IF NOT EXISTS hrm_employees (
    id              BIGSERIAL PRIMARY KEY,
    emp_no          VARCHAR(30) NOT NULL UNIQUE,
    name            VARCHAR(100) NOT NULL,
    name_en         VARCHAR(100),
    department_id   BIGINT REFERENCES hrm_departments(id),
    position_id     BIGINT REFERENCES hrm_positions(id),
    grade_id        BIGINT REFERENCES hrm_grades(id),
    employment_type VARCHAR(20) NOT NULL DEFAULT 'REGULAR',  -- REGULAR/CONTRACT/PARTTIME
    status          VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',   -- ACTIVE/LEAVE/RETIRED
    hire_date       DATE NOT NULL,
    retire_date     DATE,
    email           VARCHAR(200),
    phone_enc       TEXT,       -- AES-256-GCM 암호화
    photo_url       VARCHAR(500),
    gender          VARCHAR(10),
    birth_date      DATE,
    address         TEXT,
    bank_account    TEXT,       -- 암호화 저장
    created_by      VARCHAR(50),
    created_at      TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at      TIMESTAMP NOT NULL DEFAULT NOW()
);

-- 사원 경력
CREATE TABLE IF NOT EXISTS hrm_emp_careers (
    id           BIGSERIAL PRIMARY KEY,
    emp_id       BIGINT NOT NULL REFERENCES hrm_employees(id),
    company_name VARCHAR(200) NOT NULL,
    position     VARCHAR(100),
    start_date   DATE NOT NULL,
    end_date     DATE,
    description  TEXT
);

-- 사원 자격증
CREATE TABLE IF NOT EXISTS hrm_emp_certs (
    id          BIGSERIAL PRIMARY KEY,
    emp_id      BIGINT NOT NULL REFERENCES hrm_employees(id),
    cert_name   VARCHAR(200) NOT NULL,
    cert_no     VARCHAR(100),
    issue_date  DATE NOT NULL,
    expire_date DATE,
    issuer      VARCHAR(200)
);

-- 급여 지급 배치
CREATE TABLE IF NOT EXISTS hrm_payroll (
    id           BIGSERIAL PRIMARY KEY,
    year         INT NOT NULL,
    month        INT NOT NULL,
    dept_id      BIGINT REFERENCES hrm_departments(id),
    total_amount NUMERIC(18,2) DEFAULT 0,
    status       VARCHAR(20) NOT NULL DEFAULT 'DRAFT',  -- DRAFT/PROCESSED/APPROVED
    processed_by VARCHAR(50),
    processed_at TIMESTAMP,
    created_at   TIMESTAMP NOT NULL DEFAULT NOW(),
    UNIQUE (year, month)
);

-- 급여항목 마스터
CREATE TABLE IF NOT EXISTS hrm_salary_items (
    id          BIGSERIAL PRIMARY KEY,
    item_code   VARCHAR(30) NOT NULL UNIQUE,
    item_name   VARCHAR(100) NOT NULL,
    item_type   VARCHAR(20) NOT NULL DEFAULT 'ALLOWANCE',  -- ALLOWANCE/DEDUCTION
    is_taxable  BOOLEAN NOT NULL DEFAULT true,
    sort_order  INT NOT NULL DEFAULT 0,
    active      BOOLEAN NOT NULL DEFAULT true
);

-- 사원 급여 기준
CREATE TABLE IF NOT EXISTS hrm_emp_salaries (
    id             BIGSERIAL PRIMARY KEY,
    emp_id         BIGINT NOT NULL REFERENCES hrm_employees(id),
    base_salary    NUMERIC(15,2) NOT NULL DEFAULT 0,
    allowances     NUMERIC(15,2) NOT NULL DEFAULT 0,
    bonus          NUMERIC(15,2) NOT NULL DEFAULT 0,
    effective_date DATE NOT NULL DEFAULT CURRENT_DATE,
    UNIQUE (emp_id)
);

-- 급여 명세서
CREATE TABLE IF NOT EXISTS hrm_payslips (
    id               BIGSERIAL PRIMARY KEY,
    payroll_id       BIGINT REFERENCES hrm_payroll(id),
    emp_id           BIGINT NOT NULL REFERENCES hrm_employees(id),
    year             INT NOT NULL,
    month            INT NOT NULL,
    base_salary      NUMERIC(15,2) NOT NULL DEFAULT 0,
    total_allowance  NUMERIC(15,2) NOT NULL DEFAULT 0,
    total_deduction  NUMERIC(15,2) NOT NULL DEFAULT 0,
    net_salary       NUMERIC(15,2) NOT NULL DEFAULT 0,
    income_tax       NUMERIC(12,2) NOT NULL DEFAULT 0,
    health_ins       NUMERIC(12,2) NOT NULL DEFAULT 0,
    pension          NUMERIC(12,2) NOT NULL DEFAULT 0,
    work_days        INT NOT NULL DEFAULT 0,
    created_at       TIMESTAMP NOT NULL DEFAULT NOW(),
    UNIQUE (emp_id, year, month)
);

-- 출퇴근
CREATE TABLE IF NOT EXISTS hrm_attendance (
    id              BIGSERIAL PRIMARY KEY,
    emp_id          BIGINT NOT NULL REFERENCES hrm_employees(id),
    work_date       DATE NOT NULL,
    check_in_time   TIMESTAMP,
    check_out_time  TIMESTAMP,
    work_minutes    INT NOT NULL DEFAULT 0,
    overtime_minutes INT NOT NULL DEFAULT 0,
    status          VARCHAR(20) NOT NULL DEFAULT 'PRESENT',  -- PRESENT/ABSENT/LEAVE/HOLIDAY
    created_by      VARCHAR(50),
    UNIQUE (emp_id, work_date)
);

-- 휴가 유형
CREATE TABLE IF NOT EXISTS hrm_leave_types (
    id         BIGSERIAL PRIMARY KEY,
    type_code  VARCHAR(30) NOT NULL UNIQUE,
    type_name  VARCHAR(100) NOT NULL,
    is_paid    BOOLEAN NOT NULL DEFAULT true,
    max_days   INT NOT NULL DEFAULT 0,
    sort_order INT NOT NULL DEFAULT 0,
    active     BOOLEAN NOT NULL DEFAULT true
);

-- 휴가 신청
CREATE TABLE IF NOT EXISTS hrm_leaves (
    id             BIGSERIAL PRIMARY KEY,
    emp_id         BIGINT NOT NULL REFERENCES hrm_employees(id),
    leave_type_id  BIGINT NOT NULL REFERENCES hrm_leave_types(id),
    start_date     DATE NOT NULL,
    end_date       DATE NOT NULL,
    days           NUMERIC(5,1) NOT NULL DEFAULT 1,
    reason         TEXT,
    status         VARCHAR(20) NOT NULL DEFAULT 'PENDING',  -- PENDING/APPROVED/REJECTED/CANCELLED
    approved_by    VARCHAR(50),
    created_by     VARCHAR(50),
    created_at     TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at     TIMESTAMP NOT NULL DEFAULT NOW()
);

-- 연차 부여
CREATE TABLE IF NOT EXISTS hrm_annual_leaves (
    id         BIGSERIAL PRIMARY KEY,
    emp_id     BIGINT NOT NULL REFERENCES hrm_employees(id),
    year       INT NOT NULL,
    total_days NUMERIC(5,1) NOT NULL DEFAULT 15,
    UNIQUE (emp_id, year)
);

-- 초과근무
CREATE TABLE IF NOT EXISTS hrm_overtime (
    id         BIGSERIAL PRIMARY KEY,
    emp_id     BIGINT NOT NULL REFERENCES hrm_employees(id),
    ot_date    DATE NOT NULL,
    start_time TIME NOT NULL,
    end_time   TIME NOT NULL,
    ot_minutes INT NOT NULL DEFAULT 0,
    reason     TEXT,
    status     VARCHAR(20) NOT NULL DEFAULT 'APPROVED',
    created_by VARCHAR(50),
    created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

-- 성과 평가
CREATE TABLE IF NOT EXISTS hrm_performance_reviews (
    id          BIGSERIAL PRIMARY KEY,
    emp_id      BIGINT NOT NULL REFERENCES hrm_employees(id),
    year        INT NOT NULL,
    period      VARCHAR(20) NOT NULL DEFAULT 'ANNUAL',  -- ANNUAL/H1/H2/Q1/Q2/Q3/Q4
    final_grade VARCHAR(5),   -- S/A/B/C/D
    score       NUMERIC(5,2),
    comments    TEXT,
    status      VARCHAR(20) NOT NULL DEFAULT 'DRAFT',  -- DRAFT/SUBMITTED/COMPLETED
    created_by  VARCHAR(50),
    created_at  TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at  TIMESTAMP NOT NULL DEFAULT NOW()
);

-- MBO 목표
CREATE TABLE IF NOT EXISTS hrm_goals (
    id               BIGSERIAL PRIMARY KEY,
    emp_id           BIGINT NOT NULL REFERENCES hrm_employees(id),
    year             INT NOT NULL,
    goal_title       VARCHAR(200) NOT NULL,
    goal_desc        TEXT,
    weight           NUMERIC(5,2) NOT NULL DEFAULT 100,
    target_value     TEXT,
    actual_value     TEXT,
    achievement_rate NUMERIC(5,2),
    status           VARCHAR(20) NOT NULL DEFAULT 'IN_PROGRESS',
    created_by       VARCHAR(50),
    created_at       TIMESTAMP NOT NULL DEFAULT NOW()
);

-- 역량 마스터
CREATE TABLE IF NOT EXISTS hrm_competencies (
    id               BIGSERIAL PRIMARY KEY,
    competency_code  VARCHAR(30) NOT NULL UNIQUE,
    competency_name  VARCHAR(100) NOT NULL,
    category         VARCHAR(50),
    description      TEXT,
    sort_order       INT NOT NULL DEFAULT 0
);

-- 역량 점수
CREATE TABLE IF NOT EXISTS hrm_competency_scores (
    id              BIGSERIAL PRIMARY KEY,
    emp_id          BIGINT NOT NULL REFERENCES hrm_employees(id),
    competency_id   BIGINT NOT NULL REFERENCES hrm_competencies(id),
    year            INT NOT NULL,
    self_score      NUMERIC(3,1),
    manager_score   NUMERIC(3,1),
    peer_score      NUMERIC(3,1),
    final_score     NUMERIC(3,1),
    evaluator       VARCHAR(50),
    updated_at      TIMESTAMP NOT NULL DEFAULT NOW(),
    UNIQUE (emp_id, competency_id, year)
);

-- 채용 공고
CREATE TABLE IF NOT EXISTS hrm_job_postings (
    id              BIGSERIAL PRIMARY KEY,
    title           VARCHAR(200) NOT NULL,
    department_id   BIGINT REFERENCES hrm_departments(id),
    employment_type VARCHAR(20) NOT NULL DEFAULT 'REGULAR',
    headcount       INT NOT NULL DEFAULT 1,
    description     TEXT,
    requirements    TEXT,
    status          VARCHAR(20) NOT NULL DEFAULT 'DRAFT',  -- DRAFT/PUBLISHED/CLOSED
    start_date      DATE,
    end_date        DATE,
    created_by      VARCHAR(50),
    created_at      TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at      TIMESTAMP NOT NULL DEFAULT NOW()
);

-- 지원자
CREATE TABLE IF NOT EXISTS hrm_applicants (
    id             BIGSERIAL PRIMARY KEY,
    posting_id     BIGINT NOT NULL REFERENCES hrm_job_postings(id),
    applicant_name VARCHAR(100) NOT NULL,
    email          VARCHAR(200),
    phone          VARCHAR(30),
    resume_url     VARCHAR(500),
    cover_letter   TEXT,
    status         VARCHAR(30) NOT NULL DEFAULT 'APPLIED',  -- APPLIED/REVIEWED/INTERVIEW/OFFER/HIRED/REJECTED
    apply_date     TIMESTAMP NOT NULL DEFAULT NOW(),
    memo           TEXT,
    created_by     VARCHAR(50),
    created_at     TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at     TIMESTAMP NOT NULL DEFAULT NOW()
);

-- 면접
CREATE TABLE IF NOT EXISTS hrm_interviews (
    id             BIGSERIAL PRIMARY KEY,
    posting_id     BIGINT NOT NULL REFERENCES hrm_job_postings(id),
    applicant_id   BIGINT NOT NULL REFERENCES hrm_applicants(id),
    interview_type VARCHAR(50) NOT NULL DEFAULT 'TECHNICAL',  -- DOCUMENT/TECHNICAL/HR/FINAL
    scheduled_at   TIMESTAMP NOT NULL,
    location       VARCHAR(200),
    interviewers   TEXT,
    result         VARCHAR(20),  -- PASS/FAIL/PENDING
    notes          TEXT,
    created_by     VARCHAR(50),
    created_at     TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at     TIMESTAMP NOT NULL DEFAULT NOW()
);

-- 교육 과정
CREATE TABLE IF NOT EXISTS hrm_training_courses (
    id             BIGSERIAL PRIMARY KEY,
    course_code    VARCHAR(50) NOT NULL UNIQUE,
    course_name    VARCHAR(200) NOT NULL,
    course_type    VARCHAR(50) NOT NULL DEFAULT 'INTERNAL',  -- INTERNAL/EXTERNAL/ONLINE
    instructor     VARCHAR(100),
    description    TEXT,
    start_date     DATE,
    end_date       DATE,
    duration_hours NUMERIC(5,1) NOT NULL DEFAULT 0,
    is_legal       BOOLEAN NOT NULL DEFAULT false,
    max_attendees  INT NOT NULL DEFAULT 50,
    status         VARCHAR(20) NOT NULL DEFAULT 'SCHEDULED',
    created_by     VARCHAR(50),
    created_at     TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at     TIMESTAMP NOT NULL DEFAULT NOW()
);

-- 교육 수강
CREATE TABLE IF NOT EXISTS hrm_training_enrollments (
    id           BIGSERIAL PRIMARY KEY,
    emp_id       BIGINT NOT NULL REFERENCES hrm_employees(id),
    course_id    BIGINT NOT NULL REFERENCES hrm_training_courses(id),
    status       VARCHAR(20) NOT NULL DEFAULT 'ENROLLED',  -- ENROLLED/COMPLETED/CANCELLED
    enrolled_at  TIMESTAMP NOT NULL DEFAULT NOW(),
    completed_at DATE,
    created_by   VARCHAR(50),
    UNIQUE (emp_id, course_id)
);

-- 감사 로그
CREATE TABLE IF NOT EXISTS hrm_audit_log (
    id          BIGSERIAL PRIMARY KEY,
    actor       VARCHAR(100) NOT NULL,
    action      VARCHAR(100) NOT NULL,
    target_type VARCHAR(50),
    target_id   VARCHAR(100),
    detail      TEXT,
    ip_addr     VARCHAR(50),
    created_at  TIMESTAMP NOT NULL DEFAULT NOW()
);

-- 시스템 설정
CREATE TABLE IF NOT EXISTS hrm_settings (
    key         VARCHAR(100) PRIMARY KEY,
    value       TEXT,
    description VARCHAR(500),
    updated_by  VARCHAR(50),
    updated_at  TIMESTAMP NOT NULL DEFAULT NOW()
);

-- 인덱스
CREATE INDEX IF NOT EXISTS idx_hrm_emp_dept ON hrm_employees(department_id);
CREATE INDEX IF NOT EXISTS idx_hrm_emp_status ON hrm_employees(status);
CREATE INDEX IF NOT EXISTS idx_hrm_attendance_date ON hrm_attendance(work_date);
CREATE INDEX IF NOT EXISTS idx_hrm_leaves_emp ON hrm_leaves(emp_id);
CREATE INDEX IF NOT EXISTS idx_hrm_leaves_status ON hrm_leaves(status);
CREATE INDEX IF NOT EXISTS idx_hrm_payslips_emp ON hrm_payslips(emp_id, year, month);
CREATE INDEX IF NOT EXISTS idx_hrm_perf_emp ON hrm_performance_reviews(emp_id, year);
CREATE INDEX IF NOT EXISTS idx_hrm_goals_emp ON hrm_goals(emp_id, year);
CREATE INDEX IF NOT EXISTS idx_hrm_audit_actor ON hrm_audit_log(actor);
CREATE INDEX IF NOT EXISTS idx_hrm_audit_created ON hrm_audit_log(created_at DESC);

-- ============================================================
-- 시드 데이터
-- ============================================================

-- admin 사용자 (password: [REDACTED]
INSERT INTO hrm_users (username, password_hash, display_name, email, role, active)
VALUES ('admin', '$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy', 'HRM 관리자', 'admin@zioinfo.co.kr', 'SUPERADMIN', true)
ON CONFLICT (username) DO NOTHING;

INSERT INTO hrm_users (username, password_hash, display_name, email, role, active)
VALUES ('hr_manager', '$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy', '인사팀장', 'hrmanager@zioinfo.co.kr', 'MANAGER', true)
ON CONFLICT (username) DO NOTHING;

-- 기본 부서
INSERT INTO hrm_departments (dept_code, dept_name, sort_order) VALUES
('MGMT', '경영지원부', 1),
('DEV', '개발부', 2),
('OPS', '운영부', 3),
('SALES', '영업부', 4),
('ADMIN', '관리부', 5)
ON CONFLICT (dept_code) DO NOTHING;

-- 직책
INSERT INTO hrm_positions (position_code, position_name, sort_order) VALUES
('CEO', '대표이사', 1),
('DIR', '이사', 2),
('MGR', '부장', 3),
('TEAM_LEAD', '팀장', 4),
('SENIOR', '선임', 5),
('STAFF', '사원', 6)
ON CONFLICT (position_code) DO NOTHING;

-- 직급
INSERT INTO hrm_grades (grade_code, grade_name, grade_level, sort_order) VALUES
('G1', '1급', 1, 1),
('G2', '2급', 2, 2),
('G3', '3급', 3, 3),
('G4', '4급', 4, 4),
('G5', '5급', 5, 5),
('G6', '6급', 6, 6)
ON CONFLICT (grade_code) DO NOTHING;

-- 휴가 유형
INSERT INTO hrm_leave_types (type_code, type_name, is_paid, max_days, sort_order) VALUES
('ANNUAL', '연차', true, 15, 1),
('HALF', '반차', true, 30, 2),
('SICK', '병가', false, 60, 3),
('MATERNITY', '출산휴가', true, 90, 4),
('PARENTAL', '육아휴직', false, 365, 5),
('SPECIAL', '특별휴가', true, 5, 6)
ON CONFLICT (type_code) DO NOTHING;

-- 급여 항목
INSERT INTO hrm_salary_items (item_code, item_name, item_type, is_taxable, sort_order) VALUES
('BASE', '기본급', 'ALLOWANCE', true, 1),
('MEAL', '식대', 'ALLOWANCE', false, 2),
('TRANSPORT', '교통비', 'ALLOWANCE', false, 3),
('OVERTIME', '초과근무수당', 'ALLOWANCE', true, 4),
('INCOME_TAX', '소득세', 'DEDUCTION', false, 10),
('RESIDENT_TAX', '지방소득세', 'DEDUCTION', false, 11),
('HEALTH_INS', '건강보험', 'DEDUCTION', false, 12),
('PENSION', '국민연금', 'DEDUCTION', false, 13),
('EMPLOY_INS', '고용보험', 'DEDUCTION', false, 14)
ON CONFLICT (item_code) DO NOTHING;

-- 역량 마스터
INSERT INTO hrm_competencies (competency_code, competency_name, category, sort_order) VALUES
('LEADERSHIP', '리더십', '공통역량', 1),
('COMM', '커뮤니케이션', '공통역량', 2),
('PROBLEM', '문제해결력', '공통역량', 3),
('CUSTOMER', '고객지향성', '공통역량', 4),
('TECH', '전문기술', '직무역량', 5),
('INNOVATION', '혁신능력', '직무역량', 6)
ON CONFLICT (competency_code) DO NOTHING;

-- 법정 교육
INSERT INTO hrm_training_courses (course_code, course_name, course_type, duration_hours, is_legal, max_attendees) VALUES
('LEGAL_SEXUAL', '직장 내 성희롱 예방교육', 'ONLINE', 1.0, true, 999),
('LEGAL_DISABLED', '장애인 인식개선 교육', 'ONLINE', 1.0, true, 999),
('LEGAL_SAFETY', '산업안전보건교육', 'INTERNAL', 2.0, true, 999),
('LEGAL_PRIVACY', '개인정보보호 교육', 'ONLINE', 1.0, true, 999)
ON CONFLICT (course_code) DO NOTHING;

-- 샘플 사원 5명
INSERT INTO hrm_employees (emp_no, name, department_id, position_id, grade_id, employment_type, status, hire_date, email, created_by)
SELECT 'EMP-2024-0001', '홍길동',
       (SELECT id FROM hrm_departments WHERE dept_code='DEV'),
       (SELECT id FROM hrm_positions WHERE position_code='MGR'),
       (SELECT id FROM hrm_grades WHERE grade_code='G3'),
       'REGULAR', 'ACTIVE', '2020-03-02', 'hong@zioinfo.co.kr', 'admin'
WHERE NOT EXISTS (SELECT 1 FROM hrm_employees WHERE emp_no='EMP-2024-0001');

INSERT INTO hrm_employees (emp_no, name, department_id, position_id, grade_id, employment_type, status, hire_date, email, created_by)
SELECT 'EMP-2024-0002', '김철수',
       (SELECT id FROM hrm_departments WHERE dept_code='OPS'),
       (SELECT id FROM hrm_positions WHERE position_code='SENIOR'),
       (SELECT id FROM hrm_grades WHERE grade_code='G4'),
       'REGULAR', 'ACTIVE', '2021-07-01', 'kim@zioinfo.co.kr', 'admin'
WHERE NOT EXISTS (SELECT 1 FROM hrm_employees WHERE emp_no='EMP-2024-0002');

INSERT INTO hrm_employees (emp_no, name, department_id, position_id, grade_id, employment_type, status, hire_date, email, created_by)
SELECT 'EMP-2024-0003', '이영희',
       (SELECT id FROM hrm_departments WHERE dept_code='SALES'),
       (SELECT id FROM hrm_positions WHERE position_code='TEAM_LEAD'),
       (SELECT id FROM hrm_grades WHERE grade_code='G3'),
       'REGULAR', 'ACTIVE', '2019-01-15', 'lee@zioinfo.co.kr', 'admin'
WHERE NOT EXISTS (SELECT 1 FROM hrm_employees WHERE emp_no='EMP-2024-0003');

INSERT INTO hrm_employees (emp_no, name, department_id, position_id, grade_id, employment_type, status, hire_date, email, created_by)
SELECT 'EMP-2024-0004', '박민수',
       (SELECT id FROM hrm_departments WHERE dept_code='DEV'),
       (SELECT id FROM hrm_positions WHERE position_code='STAFF'),
       (SELECT id FROM hrm_grades WHERE grade_code='G5'),
       'REGULAR', 'ACTIVE', '2023-03-02', 'park@zioinfo.co.kr', 'admin'
WHERE NOT EXISTS (SELECT 1 FROM hrm_employees WHERE emp_no='EMP-2024-0004');

INSERT INTO hrm_employees (emp_no, name, department_id, position_id, grade_id, employment_type, status, hire_date, email, created_by)
SELECT 'EMP-2024-0005', '최지현',
       (SELECT id FROM hrm_departments WHERE dept_code='ADMIN'),
       (SELECT id FROM hrm_positions WHERE position_code='SENIOR'),
       (SELECT id FROM hrm_grades WHERE grade_code='G4'),
       'REGULAR', 'ACTIVE', '2022-05-09', 'choi@zioinfo.co.kr', 'admin'
WHERE NOT EXISTS (SELECT 1 FROM hrm_employees WHERE emp_no='EMP-2024-0005');

-- 기본 설정
INSERT INTO hrm_settings (key, value, description) VALUES
('company_name', '지오정보기술(주)', '회사명'),
('fiscal_year_start', '01', '회계연도 시작월'),
('annual_leave_base', '15', '기본 연차 일수'),
('payroll_day', '25', '급여 지급일')
ON CONFLICT (key) DO NOTHING;
