# TASK 2.1: Implement Tenant-Resolved Redshift Curated Writer

## Summary

Implement the Redshift writer that upserts synthetic KDS rows into the resolved tenant database.

The writer uses the branch's source-mirror KDS `curated` tables rather than canonical order projections.

The writer targets:

- `curated.kds_orders`
- `curated.kds_items`
- `curated.kds_item_bumps`

It must not write directly to `serving`.

## Dependencies

- TASK 1.1 (synthetic feed configuration and service contract)
- TASK 1.3 (source-to-source-mirror curated mapping)
- Redshift Foundation tenant connection factory and bootstrapped tenant template

## Files to Create

### 1. Redshift curated writer service

Create a writer equivalent to:

- `ISyntheticKdsCuratedWriter`
- `RedshiftSyntheticKdsCuratedWriter`

Exact names may differ.

### 2. SQL/upsert artifacts if needed

Create explicit SQL artifacts or query-builder helpers for upserting:

- source-mirror orders,
- source-mirror items,
- source-mirror item bumps.

Prefer readable SQL artifacts where the project pattern supports them.

## Files to Modify

### 3. Feed service implementation

Implement the main synthetic feed orchestration enough to:

1. resolve tenant mapping,
2. read source rows,
3. map source-mirror rows,
4. invoke the Redshift writer.

## Required Behavior

The Redshift writer must:

- resolve the target tenant database from `tenant_id`,
- reject missing tenant mappings,
- reject inactive tenant mappings,
- open the tenant Redshift database through the warehouse foundation,
- replace source-mirror orders by `guid`,
- replace source-mirror items by `guid`,
- replace source-mirror item bumps by `guid`,
- update changed simulator rows on later runs,
- avoid duplicate rows on repeated runs,
- write only to `curated` objects.

The writer must not:

- derive database names from naming conventions,
- accept normal user input for `tenant_database`,
- write to `serving`,
- write to S3,
- use Redshift `COPY` or `UNLOAD`.

## Acceptance Criteria

- [x] Writer resolves tenant database from `tenant_id`
- [x] Writer rejects missing tenant mapping
- [x] Writer rejects inactive tenant mapping
- [x] Writer opens the resolved Redshift tenant database through the foundation connection factory
- [x] Writer upserts `curated.kds_orders`
- [x] Writer upserts `curated.kds_items`
- [x] Writer upserts `curated.kds_item_bumps`
- [x] Writer does not write to `serving`
- [x] Writer does not use S3, `COPY`, or `UNLOAD`
- [x] Repeated writes for the same rows do not duplicate data
- [x] Solution builds without errors

## Notes

- Keep the writer small and replaceable by the future ETL worker.
- If Redshift-specific upsert limitations affect implementation, document the chosen pattern in code comments or SQL artifact names.
- Do not expand the warehouse contract in this task.
