## Task 1: Tabella `dvr_pdf` (schema + migrazione)

**Files:**
- Modify: `app/src/lib/server/db/schema.ts`
- Create: `app/migrations/2026-06-24-phase3-dvr-pdf.sql`

**Interfaces:**
- Produces: tabella/Drizzle `dvrPdf` con colonne `agenziaId` (PK, FK agenzie.id), `r2Key`, `dataHash`, `stato` (`'ready'|'generating'|'error'`), `generatoIl`, `errorMsg`, `updatedAt`.

- [ ] **Step 1: Aggiungi la tabella allo schema Drizzle**

In `app/src/lib/server/db/schema.ts`, dopo la definizione di `dvr` (e relative figlie), aggiungi:

```ts
export const dvrPdf = sqliteTable('dvr_pdf', {
	agenziaId: integer('agenzia_id')
		.primaryKey()
		.references(() => agenzie.id, { onDelete: 'cascade' }),
	r2Key: text('r2_key'),
	dataHash: text('data_hash'),
	stato: text('stato', { enum: ['ready', 'generating', 'error'] }),
	generatoIl: text('generato_il'),
	errorMsg: text('error_msg'),
	updatedAt: text('updated_at')
		.notNull()
		.default(sql`CURRENT_TIMESTAMP`)
});
```

- [ ] **Step 2: Scrivi la migrazione SQL**

Crea `app/migrations/2026-06-24-phase3-dvr-pdf.sql`:

```sql
-- 2026-06-24 Fase 3 — tabella stato PDF per agenzia (cache PDF su R2)
CREATE TABLE IF NOT EXISTS dvr_pdf (
  agenzia_id  INTEGER PRIMARY KEY REFERENCES agenzie(id) ON DELETE CASCADE,
  r2_key      TEXT,
  data_hash   TEXT,
  stato       TEXT CHECK (stato IN ('ready','generating','error')),
  generato_il TEXT,
  error_msg   TEXT,
  updated_at  TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
```

- [ ] **Step 3: Applica a locale e verifica**

```bash
cd "/Users/paolo/Server/Siti Web/Lavoro/Inlumia/Alleanza"
sqlite3 import-tools/db/dev.db < app/migrations/2026-06-24-phase3-dvr-pdf.sql
sqlite3 import-tools/db/dev.db ".schema dvr_pdf"
```
Atteso: lo schema della tabella stampato senza errori.

- [ ] **Step 4: Applica a prod e verifica**

```bash
turso db shell alleanza-dvr-prod < app/migrations/2026-06-24-phase3-dvr-pdf.sql
printf "SELECT name FROM sqlite_master WHERE type='table' AND name='dvr_pdf';\n" | turso db shell alleanza-dvr-prod
```
Atteso: stampa `dvr_pdf`.

- [ ] **Step 5: Type-check**

Run: `cd app && npm run check`
Expected: 0 errori (warning preesistenti ammessi).

- [ ] **Step 6: Commit**

```bash
git add app/src/lib/server/db/schema.ts app/migrations/2026-06-24-phase3-dvr-pdf.sql
git commit -m "PDF: tabella dvr_pdf (stato/cache PDF per agenzia)"
```

---

