-- 1. FITTING 상세 테이블
CREATE TABLE IF NOT EXISTS fitting_details (
    id SERIAL PRIMARY KEY,
    material_id INTEGER REFERENCES materials(id) ON DELETE CASCADE,
    file_id INTEGER REFERENCES files(id) ON DELETE CASCADE,
    
    -- 피팅 타입 정보
    fitting_type VARCHAR(50),
    fitting_subtype VARCHAR(50),
    
    -- 연결 방식
    connection_method VARCHAR(50),
    connection_code VARCHAR(50),
    
    -- 압력 등급
    pressure_rating VARCHAR(50),
    max_pressure VARCHAR(50),
    
    -- 제작 방법
    manufacturing_method VARCHAR(50),
    
    -- 재질 정보
    material_standard VARCHAR(100),
    material_grade VARCHAR(100),
    material_type VARCHAR(50),
    
    -- 사이즈 정보
    main_size VARCHAR(50),
    reduced_size VARCHAR(50),
    
    -- 신뢰도
    classification_confidence FLOAT,
    
    -- 추가 정보
    additional_info JSONB,
    
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 2. VALVE 상세 테이블
CREATE TABLE IF NOT EXISTS valve_details (
    id SERIAL PRIMARY KEY,
    material_id INTEGER REFERENCES materials(id) ON DELETE CASCADE,
    file_id INTEGER REFERENCES files(id) ON DELETE CASCADE,
    
    -- 밸브 타입 정보
    valve_type VARCHAR(50),
    valve_subtype VARCHAR(50),
    actuator_type VARCHAR(50),
    
    -- 연결 방식
    connection_method VARCHAR(50),
    
    -- 압력 등급
    pressure_rating VARCHAR(50),
    pressure_class VARCHAR(50),
    
    -- 재질 정보
    body_material VARCHAR(100),
    trim_material VARCHAR(100),
    
    -- 사이즈 정보
    size_inches VARCHAR(50),
    
    -- 특수 사양
    fire_safe BOOLEAN,
    low_temp_service BOOLEAN,
    special_features JSONB,
    
    -- 신뢰도
    classification_confidence FLOAT,
    
    -- 추가 정보
    additional_info JSONB,
    
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 3. FLANGE 상세 테이블
CREATE TABLE IF NOT EXISTS flange_details (
    id SERIAL PRIMARY KEY,
    material_id INTEGER REFERENCES materials(id) ON DELETE CASCADE,
    file_id INTEGER REFERENCES files(id) ON DELETE CASCADE,
    
    -- 플랜지 타입
    flange_type VARCHAR(50),
    facing_type VARCHAR(50),
    
    -- 압력 등급
    pressure_rating VARCHAR(50),
    
    -- 재질 정보
    material_standard VARCHAR(100),
    material_grade VARCHAR(100),
    
    -- 사이즈 정보
    size_inches VARCHAR(50),
    bolt_hole_count INTEGER,
    bolt_hole_size VARCHAR(50),
    
    -- 신뢰도
    classification_confidence FLOAT,
    
    -- 추가 정보
    additional_info JSONB,
    
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 4. BOLT 상세 테이블
CREATE TABLE IF NOT EXISTS bolt_details (
    id SERIAL PRIMARY KEY,
    material_id INTEGER REFERENCES materials(id) ON DELETE CASCADE,
    file_id INTEGER REFERENCES files(id) ON DELETE CASCADE,
    
    -- 볼트 타입
    bolt_type VARCHAR(50),
    thread_type VARCHAR(50),
    
    -- 사양 정보
    diameter VARCHAR(50),
    length VARCHAR(50),
    
    -- 재질 정보
    material_standard VARCHAR(100),
    material_grade VARCHAR(100),
    coating_type VARCHAR(100),
    
    -- 너트/와셔 정보
    includes_nut BOOLEAN,
    includes_washer BOOLEAN,
    nut_type VARCHAR(50),
    washer_type VARCHAR(50),
    
    -- 신뢰도
    classification_confidence FLOAT,
    
    -- 추가 정보
    additional_info JSONB,
    
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 5. GASKET 상세 테이블
CREATE TABLE IF NOT EXISTS gasket_details (
    id SERIAL PRIMARY KEY,
    material_id INTEGER REFERENCES materials(id) ON DELETE CASCADE,
    file_id INTEGER REFERENCES files(id) ON DELETE CASCADE,
    
    -- 가스켓 타입
    gasket_type VARCHAR(50),
    gasket_subtype VARCHAR(50),
    
    -- 재질 정보
    material_type VARCHAR(100),
    filler_material VARCHAR(100),
    
    -- 사이즈 및 등급
    size_inches VARCHAR(50),
    pressure_rating VARCHAR(50),
    thickness VARCHAR(50),
    
    -- 특수 사양
    temperature_range VARCHAR(100),
    fire_safe BOOLEAN,
    
    -- 신뢰도
    classification_confidence FLOAT,
    
    -- 추가 정보
    additional_info JSONB,
    
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 6. INSTRUMENT 상세 테이블
CREATE TABLE IF NOT EXISTS instrument_details (
    id SERIAL PRIMARY KEY,
    material_id INTEGER REFERENCES materials(id) ON DELETE CASCADE,
    file_id INTEGER REFERENCES files(id) ON DELETE CASCADE,
    
    -- 계장품 타입
    instrument_type VARCHAR(50),
    instrument_subtype VARCHAR(50),
    
    -- 측정 사양
    measurement_type VARCHAR(50),
    measurement_range VARCHAR(100),
    accuracy VARCHAR(50),
    
    -- 연결 정보
    connection_type VARCHAR(50),
    connection_size VARCHAR(50),
    
    -- 재질 정보
    body_material VARCHAR(100),
    wetted_parts_material VARCHAR(100),
    
    -- 전기 사양
    electrical_rating VARCHAR(100),
    output_signal VARCHAR(50),
    
    -- 신뢰도
    classification_confidence FLOAT,
    
    -- 추가 정보
    additional_info JSONB,
    
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 인덱스 생성
CREATE INDEX idx_fitting_details_material_id ON fitting_details(material_id);
CREATE INDEX idx_fitting_details_file_id ON fitting_details(file_id);
CREATE INDEX idx_fitting_details_type ON fitting_details(fitting_type);

CREATE INDEX idx_valve_details_material_id ON valve_details(material_id);
CREATE INDEX idx_valve_details_file_id ON valve_details(file_id);
CREATE INDEX idx_valve_details_type ON valve_details(valve_type);

CREATE INDEX idx_flange_details_material_id ON flange_details(material_id);
CREATE INDEX idx_flange_details_file_id ON flange_details(file_id);

CREATE INDEX idx_bolt_details_material_id ON bolt_details(material_id);
CREATE INDEX idx_bolt_details_file_id ON bolt_details(file_id);

CREATE INDEX idx_gasket_details_material_id ON gasket_details(material_id);
CREATE INDEX idx_gasket_details_file_id ON gasket_details(file_id);

CREATE INDEX idx_instrument_details_material_id ON instrument_details(material_id);
CREATE INDEX idx_instrument_details_file_id ON instrument_details(file_id); 