-- Seed data provides one complete end-to-end example for the frontend:
-- a checklist template, lookup values, image policy, export profile, and a few
-- app-level settings. The goal is not exhaustive coverage but a realistic shape
-- that exercises the dynamic form renderer and admin configuration flow.

USE check_list;

-- Register the template shell first so later version rows can reference it.
INSERT INTO templates (code, name, description)
VALUES (
  'incoming-inspection',
  'Incoming Inspection Checklist',
  'PoC template for supplier or incoming goods quality inspection.'
)
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  description = VALUES(description);

SET @template_id = (SELECT id FROM templates WHERE code = 'incoming-inspection');

-- The JSON payload below is intentionally close to the frontend contract. That
-- makes it easy to inspect how the browser creates fields, validation hints, and
-- attachment requirements without another transformation layer in the backend.
INSERT INTO template_versions (
  template_id,
  version_number,
  status,
  definition_json,
  published_at
)
VALUES (
  @template_id,
  1,
  'active',
  '{
    "templateId": "incoming-inspection",
    "templateName": "Incoming Inspection Checklist",
    "version": 1,
    "reportNumberPattern": "QC-{yyyy}-{seq:4}",
    "exportProfileCode": "default-report-export",
    "imageRuleCode": "standard-mobile-images",
    "sections": [
      {
        "id": "header",
        "title": "Report Header",
        "type": "group",
        "fields": [
          {
            "id": "reportNumber",
            "label": "Report Number",
            "type": "text",
            "required": true,
            "readOnly": false
          },
          {
            "id": "inspectionDate",
            "label": "Inspection Date",
            "type": "date",
            "required": true
          },
          {
            "id": "supplierName",
            "label": "Supplier",
            "type": "text",
            "required": true
          },
          {
            "id": "batchNumber",
            "label": "Batch Number",
            "type": "text",
            "required": false
          }
        ]
      },
      {
        "id": "check-items",
        "title": "Inspection Items",
        "type": "group",
        "fields": [
          {
            "id": "packagingCondition",
            "label": "Packaging Condition",
            "type": "lookup",
            "lookupCode": "pass-fail",
            "required": true
          },
          {
            "id": "labelCheck",
            "label": "Label Verification",
            "type": "lookup",
            "lookupCode": "pass-fail",
            "required": true
          },
          {
            "id": "quantityVerified",
            "label": "Quantity Verified",
            "type": "number",
            "required": true,
            "validation": {
              "min": 0
            }
          },
          {
            "id": "damageFound",
            "label": "Visible Damage Found",
            "type": "checkbox",
            "required": false,
            "defaultValue": false
          },
          {
            "id": "damagePhoto",
            "label": "Damage Photo",
            "type": "attachment",
            "requiredWhen": {
              "field": "damageFound",
              "equals": true
            },
            "maxAttachments": 3
          },
          {
            "id": "inspectorComment",
            "label": "Inspector Comment",
            "type": "comment",
            "required": false,
            "maxLength": 1000
          }
        ]
      }
    ]
  }',
  NOW()
)
ON DUPLICATE KEY UPDATE
  status = VALUES(status),
  definition_json = VALUES(definition_json),
  published_at = VALUES(published_at);

-- Lookups are seeded separately because multiple templates may eventually reuse
-- the same option sets, such as pass/fail or standardized status lists.
INSERT INTO lookup_sets (code, name)
VALUES
  ('pass-fail', 'Pass/Fail'),
  ('draft-status', 'Draft Status')
ON DUPLICATE KEY UPDATE
  name = VALUES(name);

SET @pass_fail_id = (SELECT id FROM lookup_sets WHERE code = 'pass-fail');
SET @draft_status_id = (SELECT id FROM lookup_sets WHERE code = 'draft-status');

INSERT INTO lookup_values (lookup_set_id, value, label, sort_order, is_default)
VALUES
  (@pass_fail_id, 'pass', 'Pass', 1, 1),
  (@pass_fail_id, 'fail', 'Fail', 2, 0),
  (@draft_status_id, 'draft', 'Draft', 1, 1),
  (@draft_status_id, 'in_progress', 'In Progress', 2, 0),
  (@draft_status_id, 'ready_for_export', 'Ready for Export', 3, 0),
  (@draft_status_id, 'exported', 'Exported', 4, 0),
  (@draft_status_id, 'archived', 'Archived', 5, 0)
ON DUPLICATE KEY UPDATE
  label = VALUES(label),
  sort_order = VALUES(sort_order),
  is_default = VALUES(is_default);

-- The image rule row is the configuration edited by the administrator UI.
INSERT INTO image_rules (
  code,
  name,
  allowed_mime_types_json,
  max_file_size_bytes,
  max_width_px,
  max_height_px,
  jpeg_quality,
  oversize_behavior,
  max_attachments_per_field,
  is_active
)
VALUES (
  'standard-mobile-images',
  'Standard Mobile Image Policy',
  '["image/jpeg", "image/png", "image/webp"]',
  5242880,
  1920,
  1920,
  82,
  'auto_optimize',
  5,
  1
)
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  allowed_mime_types_json = VALUES(allowed_mime_types_json),
  max_file_size_bytes = VALUES(max_file_size_bytes),
  max_width_px = VALUES(max_width_px),
  max_height_px = VALUES(max_height_px),
  jpeg_quality = VALUES(jpeg_quality),
  oversize_behavior = VALUES(oversize_behavior),
  max_attachments_per_field = VALUES(max_attachments_per_field),
  is_active = VALUES(is_active);

-- Export profile settings are placeholders for the later XLSX/ZIP phase.
INSERT INTO export_profiles (
  code,
  name,
  zip_image_dir,
  excel_sheet_name,
  include_template_version,
  include_export_timestamp,
  is_active
)
VALUES (
  'default-report-export',
  'Default Report Export',
  'images',
  'Checklist',
  1,
  1,
  1
)
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  zip_image_dir = VALUES(zip_image_dir),
  excel_sheet_name = VALUES(excel_sheet_name),
  include_template_version = VALUES(include_template_version),
  include_export_timestamp = VALUES(include_export_timestamp),
  is_active = VALUES(is_active);
