# MF919 Seed Files Alignment - Summary

## Overview
Updated MF919 parameter seed files to align with current database structure and resolve schema mismatches identified during comparison with `currentdb-data.txt`.

## Files Modified

### 1. Seed Files

#### `/apps/da_product_app/priv/repo/seeds/mf919_parameter_categories.exs`
- **Change:** Integrated MF919 categories under existing "Device Setup" parent category
- **Reason:** Maintain consistency with current DB hierarchy structure
- **Impact:** MF919 categories now appear as children of Device Setup (id=20, code=device_setup)

#### `/apps/da_product_app/priv/repo/seeds/mf919_default_template.exs`
- **Change:** Updated to reference existing "MF919 Default Configuration" template (id=3)
- **Reason:** Current DB already has this template; avoid duplicate creation
- **Impact:** Template values populate existing template instead of creating a new one

#### `/apps/da_product_app/priv/repo/seeds/mf919_parameter_definitions.exs`
- **Status:** No changes required - already compatible with schema
- **Contains:** 47 parameter definitions organized into 8 categories

#### `/apps/da_product_app/priv/repo/seeds/mf919_merchant_overlay.exs`
- **Status:** No changes required - correctly uses DeviceParameterOverride

### 2. New Migration

#### `/priv/repo/migrations/20260310000001_align_parameter_definitions_schema.exs`
#### `/apps/da_product_app/priv/repo/migrations/20260310000001_align_parameter_definitions_schema.exs`

- **Purpose:** Fix schema-migration mismatch in parameter_definitions table
- **Adds Missing Fields:**
  - `key` (string) - unique identifier for parameters
  - `is_required` (boolean) - marks required parameters
  - `is_system` (boolean) - distinguishes system vs user parameters
  - `is_encrypted` (boolean) - indicates encrypted parameters
  - `display_order` (integer) - controls UI display order
  - `is_active` (boolean) - replaces `active` field
  
- **Safe Execution:** Uses `add_if_not_exists` and conditional SQL to handle existing columns

### 3. Documentation

#### `/docs/MF919_SEED_ALIGNMENT.md` (NEW)
Comprehensive documentation covering:
- Current DB structure analysis
- Changes made and rationale
- Category hierarchy diagram
- Execution order and instructions
- Verification checklist
- Migration path for existing systems

## Key Integration Points

### Category Hierarchy
```
Device Setup (existing, id=20)
└── MF919 Device Setup (new root for MF919)
    ├── MF919 Base / Merchant Defaults
    ├── MF919 Merchant Overlay
    ├── MF919 Communication
    ├── MF919 Transactions
    ├── MF919 Printing
    ├── MF919 PINPAD
    ├── MF919 Scanner
    └── MF919 Security
```

### Template Structure
- **Existing Template:** "MF919 Default Configuration" (id=3, vendor=Morefun, model=MF919)
- **Template Values:** ~42 base parameters (excludes MERCHANT_ID, TERMINAL_ID, BATCH_NO)
- **Overrides:** Merchant-specific values via DeviceParameterOverride

### Parameter Organization
- **47 total parameters** defined across 8 categories
- **3 merchant overlay** parameters (applied per-terminal)
- **44 template parameters** (shared defaults)

## Execution Instructions

```bash
# 1. Run schema alignment migration
mix ecto.migrate

# 2. Seed MF919 categories
mix run apps/da_product_app/priv/repo/seeds/mf919_parameter_categories.exs

# 3. Seed MF919 parameter definitions
mix run apps/da_product_app/priv/repo/seeds/mf919_parameter_definitions.exs

# 4. Populate MF919 default template
mix run apps/da_product_app/priv/repo/seeds/mf919_default_template.exs

# 5. (Optional) Apply merchant overlay to a terminal
SAMPLE_TERMINAL_ID=1 mix run apps/da_product_app/priv/repo/seeds/mf919_merchant_overlay.exs
```

## Verification

After running seeds, verify:
- [ ] MF919 categories appear under Device Setup in the UI
- [ ] Template id=3 has ~42 parameter values
- [ ] Parameter definitions have correct category associations
- [ ] Test terminal has merchant-specific overrides (if overlay script was run)

## Benefits

1. **Consistency:** Aligns with existing DB structure and conventions
2. **Maintainability:** Uses existing template and category hierarchy
3. **Schema Safety:** Migration ensures schema-table alignment before seeding
4. **Flexibility:** Template + override pattern supports reusable defaults with terminal-specific customization
5. **Zero Disruption:** Safe for existing systems (creates only missing data)

## Related Files

- Current DB: `docs/tms_mf919_parameter_samples/currentdb-data.txt`
- Schemas: `lib/da_product_app/parameter_management/*.ex`
- Original Parameter Migrations: `priv/repo/migrations/202401010000[19-20]_*.exs`

## Testing Recommendations

1. Test in development with fresh database
2. Test with existing database having partial MF919 data
3. Verify UI displays category hierarchy correctly
4. Test parameter resolution: template → override precedence
5. Validate merchant overlay creates correct DeviceParameterOverride records
