# TASK 1.3: Create EF Migration for New Entities

## Summary

Generate the Entity Framework Core migration for the `MonitorSpec` and `MonitorRun` entities created in TASK 1.1 and TASK 1.2.

## Dependencies

- TASK 1.1 (MonitorSpec entity)
- TASK 1.2 (MonitorRun entity)

## Commands to Execute

### 1. Generate the migration

```bash
cd backend/Kds.Ai.Data
./Add-Migration.ps1 AddMonitorSpecAndRuns
```

### 2. Review the generated migration

The generated migration file should be at:
`backend/Kds.Ai.Data/Migrations/<timestamp>_AddMonitorSpecAndRuns.cs`

Expected tables:
- `MonitorSpecs` - Main monitor configuration table
- `MonitorRuns` - Execution history table
- `MonitorRunAlertEvents` - Join table for MonitorRun <-> AlertEvent many-to-many

### 3. Verify migration contents

The migration should include:

**MonitorSpecs table:**
- `Id` (char(36), PK)
- `Name` (varchar(200), NOT NULL, indexed)
- `SystemPrompt` (longtext, NOT NULL)
- `Configuration` (longtext, nullable)
- `State` (varchar(50), NOT NULL, indexed) - enum stored as string
- `CreatedAt` (datetime, NOT NULL)
- `UpdatedAt` (datetime, nullable)
- `LastRunAt` (datetime, nullable)

**MonitorRuns table:**
- `Id` (char(36), PK)
- `MonitorSpecId` (char(36), NOT NULL, FK, indexed)
- `Status` (varchar(50), NOT NULL, indexed) - enum stored as string
- `StartedAt` (datetime, NOT NULL, indexed)
- `CompletedAt` (datetime, nullable)
- `AnalysisWindowStart` (datetime, NOT NULL)
- `AnalysisWindowEnd` (datetime, NOT NULL)
- `ResultSummary` (longtext, nullable)
- `ErrorMessage` (varchar(2000), nullable)
- `AnomaliesDetected` (int, NOT NULL)
- `TokensUsed` (int, nullable)
- `DurationMs` (bigint, nullable)
- Composite index on (MonitorSpecId, StartedAt)

**MonitorRunAlertEvents join table:**
- `MonitorRunId` (char(36), FK to MonitorRuns)
- `AlertEventId` (char(36), FK to AlertEvents)
- Composite PK on (MonitorRunId, AlertEventId)

### 4. Migration application

Migration will be applied automatically during AppHost startup - no manual steps needed.

## Files Created

- `backend/Kds.Ai.Data/Migrations/<timestamp>_AddMonitorSpecAndRuns.cs`
- `backend/Kds.Ai.Data/Migrations/<timestamp>_AddMonitorSpecAndRuns.Designer.cs`
- Updated `backend/Kds.Ai.Data/Migrations/KdsAiDbContextModelSnapshot.cs`

## Acceptance Criteria

- [ ] Migration generates without errors using `Add-Migration.ps1`
- [ ] Migration includes `MonitorSpecs`, `MonitorRuns`, and `MonitorRunAlertEvents` tables
- [ ] All indexes are properly defined
- [ ] Foreign key constraints are properly defined
- [ ] Migration will be auto-applied at AppHost startup

## Notes

- Review the generated migration before committing to ensure it matches expectations
- The migration should use MySQL-specific column types (char for GUIDs, longtext, etc.)
- If there are issues with the generated migration, fix the entity configurations rather than manually editing the migration
- Use the `Add-Migration.ps1` script in `backend/Kds.Ai.Data/` - it handles startup project and output directory configuration
