## 1. Database Schema & Migration

- [x] 1.1 Add `charts` table to Drizzle schema (`src/lib/db/schema.ts`) with columns: `id`, `name` (unique), `created_at`
- [x] 1.2 Add `chart_id` foreign key column to `candles` table in schema, replace `time` unique constraint with composite unique on `(chart_id, time)`
- [x] 1.3 Add `chart_id` foreign key column to `annotations` table in schema
- [x] 1.4 Generate Drizzle migration files (`npx drizzle-kit generate`)
- [x] 1.5 Write data migration logic: if existing candles exist, create a default "Imported Data" chart and assign all existing candles and annotations to it
- [x] 1.6 Test migration against existing database with data

## 2. Charts API Endpoints

- [x] 2.1 Create `GET /api/charts` endpoint — returns all charts ordered by `created_at` desc
- [x] 2.2 Create `DELETE /api/charts/[id]` endpoint — deletes chart + cascading candles and annotations in a transaction

## 3. Upload Endpoint Changes

- [x] 3.1 Modify `POST /api/upload` to create a new chart named from the uploaded filename (strip `.csv` extension)
- [x] 3.2 Add duplicate name handling — append numeric suffix if chart name already exists
- [x] 3.3 Insert candles with the new chart's `chart_id` instead of deleting all existing candles
- [x] 3.4 Return chart `id` and `name` in the upload response JSON

## 4. Candles & Annotations API Scoping

- [x] 4.1 Modify `GET /api/candles` to accept `?chartId=` query param and filter by chart_id (fall back to most recent chart if omitted)
- [x] 4.2 Modify `GET /api/annotations` to accept `?chartId=` query param and filter by chart_id (fall back to most recent chart if omitted)
- [x] 4.3 Modify `POST /api/annotations` to require `chart_id` in request body and store it
- [x] 4.4 Modify `GET /api/export` to accept `?chartId=` query param and scope exported annotations to that chart

## 5. Chart Selector UI Component

- [x] 5.1 Create a `ChartSelector` component with dropdown listing all charts (name + created date), positioned in the sidebar between header and file upload
- [x] 5.2 Add delete button per chart in the dropdown with confirmation dialog
- [x] 5.3 Show "No charts — upload a CSV to get started" placeholder when no charts exist

## 6. Frontend State & Data Flow

- [x] 6.1 Add `activeChartId` and `charts` state to `page.tsx`
- [x] 6.2 Fetch charts list on mount via `GET /api/charts`, auto-select the most recent chart
- [x] 6.3 Pass `activeChartId` to `CandleChart` component — update it to fetch candles/annotations with `?chartId=` param
- [x] 6.4 Update `handleUploadSuccess` to receive the new chart from the upload response, add it to `charts` state, and set it as `activeChartId`
- [x] 6.5 Update annotation create/delete flows to include `chart_id` in requests
- [x] 6.6 When `activeChartId` changes, refetch candles and annotations for the new chart
- [x] 6.7 Update export handler to include `?chartId=` in the export URL

## 7. UI Polish

- [x] 7.1 Restyle "Manage Annotation Types" link — replace `text-blue-600 hover:text-blue-800 underline` with `text-muted-foreground hover:text-foreground` and remove underline
- [x] 7.2 Verify chart selector and all components render correctly in both light and dark themes

## 8. Testing & Verification

- [x] 8.1 Test full workflow: upload CSV -> chart created -> switch charts -> annotations scoped correctly
- [x] 8.2 Test migration: existing database with candles/annotations migrates to default chart
- [x] 8.3 Test chart deletion: candles and annotations cascade-deleted
- [x] 8.4 Test edge cases: upload duplicate filename, delete last chart, upload with no charts existing
- [x] 8.5 Run build (`npm run build`) and fix any type errors
