{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "CSV Import Configuration Schema",
    "description": "CSVインポート設定ファイルのJSONスキーマ定義",
    "type": "object",
    "required": [
        "table_name",
        "display_name",
        "type",
        "supported_modes",
        "key_fields",
        "required_fields",
        "field_labels"
    ],
    "properties": {
        "table_name": {
            "type": "string",
            "description": "対象テーブル名"
        },
        "display_name": {
            "type": "string",
            "description": "画面表示用の名称"
        },
        "description": {
            "type": "string",
            "description": "テーブルの説明"
        },
        "type": {
            "type": "string",
            "enum": ["single_table", "business_data"],
            "description": "インポートタイプ"
        },
        "supported_modes": {
            "type": "array",
            "items": {
                "type": "string",
                "enum": ["insert", "update", "delete"]
            },
            "description": "サポートするインポートモード"
        },
        "key_fields": {
            "type": "array",
            "items": {
                "type": "string"
            },
            "description": "キー項目（UPDATE/DELETE時に使用）"
        },
        "required_fields": {
            "type": "array",
            "items": {
                "type": "string"
            },
            "description": "必須項目"
        },
        "unique_fields": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z_][a-zA-Z0-9_]*$": {
                    "type": "object",
                    "required": ["table", "column", "message"],
                    "properties": {
                        "table": {
                            "type": "string",
                            "description": "チェック対象テーブル"
                        },
                        "column": {
                            "type": "string",
                            "description": "チェック対象カラム"
                        },
                        "message": {
                            "type": "string",
                            "description": "重複時のエラーメッセージ"
                        },
                        "modes": {
                            "type": "array",
                            "items": {
                                "type": "string",
                                "enum": ["insert", "update", "delete"]
                            },
                            "description": "チェック対象モード"
                        },
                        "allow_null": {
                            "type": "boolean",
                            "description": "NULL値の重複を許可するか"
                        }
                    }
                }
            },
            "description": "ユニーク制約設定"
        },
        "field_labels": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z_][a-zA-Z0-9_]*$": {
                    "type": "string"
                }
            },
            "description": "フィールドの表示ラベル"
        },
        "field_types": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z_][a-zA-Z0-9_]*$": {
                    "type": "string",
                    "enum": ["varchar", "int", "bigint", "decimal", "text", "timestamp", "date", "tinyint"]
                }
            },
            "description": "フィールドのデータ型"
        },
        "enum_values": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z_][a-zA-Z0-9_]*$": {
                    "type": "object",
                    "patternProperties": {
                        "^[0-9]+$": {
                            "type": "string"
                        }
                    }
                }
            },
            "description": "Enum値の定義"
        },
        "foreign_key_mappings": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z_][a-zA-Z0-9_]*$": {
                    "type": "object",
                    "required": [
                        "target_field",
                        "reference_table",
                        "reference_key_column",
                        "reference_value_column"
                    ],
                    "properties": {
                        "target_field": {
                            "type": "string",
                            "description": "変換先フィールド名"
                        },
                        "reference_table": {
                            "type": "string",
                            "description": "参照テーブル名"
                        },
                        "reference_key_column": {
                            "type": "string",
                            "description": "参照テーブルのキーカラム名"
                        },
                        "reference_value_column": {
                            "type": "string",
                            "description": "参照テーブルの値カラム名"
                        },
                        "required": {
                            "type": "boolean",
                            "description": "必須かどうか"
                        },
                        "error_message": {
                            "type": "string",
                            "description": "参照エラー時のメッセージ"
                        }
                    }
                }
            },
            "description": "外部キー参照マッピング設定"
        },
        "composite_key_handling": {
            "type": "object",
            "properties": {
                "update_mode": {
                    "type": "string",
                    "enum": ["primary_key_only", "composite_key", "auto_detect"],
                    "description": "更新時のキー処理方式"
                },
                "delete_mode": {
                    "type": "string",
                    "enum": ["primary_key_only", "composite_key", "auto_detect"],
                    "description": "削除時のキー処理方式"
                },
                "primary_key_field": {
                    "type": "string",
                    "description": "主キーフィールド名"
                },
                "description": {
                    "type": "string",
                    "description": "複合キー処理の説明"
                }
            },
            "description": "複合キー処理設定"
        },
        "validation_rules": {
            "type": "object",
            "patternProperties": {
                "^[a-zA-Z_][a-zA-Z0-9_]*$": {
                    "type": "object",
                    "properties": {
                        "required": {
                            "type": "boolean",
                            "description": "必須かどうか"
                        },
                        "max_length": {
                            "type": "integer",
                            "description": "最大文字数"
                        },
                        "min_length": {
                            "type": "integer",
                            "description": "最小文字数"
                        },
                        "pattern": {
                            "type": "string",
                            "description": "正規表現パターン"
                        },
                        "message": {
                            "type": "string",
                            "description": "バリデーションエラーメッセージ"
                        },
                        "enum": {
                            "type": "array",
                            "items": {
                                "type": ["string", "number"]
                            },
                            "description": "許可される値の配列"
                        },
                        "foreign_key": {
                            "type": "boolean",
                            "description": "外部キー参照フィールドかどうか"
                        },
                        "unique": {
                            "type": "boolean",
                            "description": "ユニーク制約があるかどうか"
                        }
                    }
                }
            },
            "description": "バリデーションルール"
        }
    }
}