# GSD: Fix amc_id encode error in EnrichAssetFromAPI
## File: `cmms_import.go`

---

## PROBLEM

```
enrich: update asset_master: failed to encode args[49]: unable to encode 190
into text format for text (OID 25): cannot find encode plan
```

`args[49]` = `$50` = `amc_id` in the UPDATE.
`amc_id` column in `ic3_asset_master` is type `TEXT` but we're passing an `int`.
pgx cannot auto-convert int → text.

Same issue likely affects other int fields passed to text columns:
- `$50` amc_id (TEXT) ← passing nullInt(det.AмcID) which is int
- `$47` fk_company_id — check column type
- `$56` asset_contract_id — check column type

---

## FIX

In `EnrichAssetFromAPI`, find the big UPDATE ic3_asset_master call.

Find these three argument lines and replace:

```go
// FIND and REPLACE these lines in the args list:

// BEFORE:
nullInt(det.AмcID),          // $50
// AFTER:
nullStrFromInt(det.AмcID),   // $50 — amc_id is TEXT column

// BEFORE:
nullInt(det.FkCompanyID),    // $47
// AFTER:
nullInt(det.FkCompanyID),    // $47 — keep as int, fk_company_id is INTEGER

// BEFORE:
nullInt(d.AssetContractID),  // $56
// AFTER:
nullInt(d.AssetContractID),  // $56 — keep as int
```

---

## ADD helper function

Add this helper anywhere in `cmms_import.go`:

```go
// nullStrFromInt converts int to string for TEXT columns, returns nil if 0
func nullStrFromInt(i int) any {
    if i == 0 {
        return nil
    }
    return strconv.Itoa(i)
}
```

---

## ALSO CHECK — verify column types in ic3_asset_master

Run this in pgAdmin to confirm which columns are TEXT vs INTEGER:

```sql
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'ic3_asset_master'
AND column_name IN (
    'amc_id', 'fk_company_id', 'asset_contract_id',
    'in_contract_type', 'contract_status', 'is_existing_contract',
    'approval_required', 'workorder_required',
    'is_main_asset', 'is_facility'
)
ORDER BY column_name;
```

For any column that is `text` but receives an int value,
use `nullStrFromInt()` instead of `nullInt()`.

Common TEXT columns in ic3_asset_master that receive int values:
- `amc_id` → use `nullStrFromInt(det.AмcID)`
- `asset_contract_id` → check type, may need `nullStrFromInt`
- `in_contract_type` → check type
- `contract_status` → check type

---

## VERIFY

```bash
curl -X POST "http://localhost:9090/api/admin/cmms/sync/Delhi%20Cantt%20Naraina_Trans_10" \
  -H "Authorization: Bearer <token>"
```

Expected: `{"status":"synced","cmms_asset_id":"Delhi Cantt Naraina_Trans_10","traceability_id":577842}`
