# Quick Start Guide

## Setup (5 minutes)

### 1. Install Dependencies

```bash
cd /var/www/html/W_CartecInstalador/api_implementation/cartec_api
npm install
```

### 2. Configure Database

```bash
# Copy example env
cp ENV_SETUP.md .env  # Copy the env vars from the markdown

# Or create .env directly:
cat > .env << 'EOF'
PORT=3000
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=password
DB_NAME=revisa
DB_CONNECTION_LIMIT=10
NODE_ENV=development
EOF
```

### 3. Import Stored Procedure

```bash
mysql -u root -p revisa < sql/prc_api_create_OT.sql
```

### 4. Start Server

```bash
npm start
```

## Test the API

### Health Check

```bash
curl http://localhost:3000/api/health
```

Expected:
```json
{"status":"ok","timestamp":"2024-01-01T12:00:00.000Z"}
```

### Create OT (No Images)

```bash
curl -X POST http://localhost:3000/api/ot/create \
  -H "Content-Type: application/json" \
  -d @request_example.json
```

### Create OT (With Signature and Images)

```bash
# 1. Convert your images to base64
base64 -w 0 /path/to/signature.png > signature.b64
base64 -w 0 /path/to/vin_photo.jpg > vin.b64

# 2. Create request with signature and images
cat > request_with_images.json << 'EOF'
{
  "cliente": {
    "rut": "12345678-9",
    "nombre": "Test Client",
    "direccion": "Test St 123",
    "comuna": "Santiago",
    "telefono": "+56912345678",
    "email": "test@test.cl"
  },
  "tecnico": {
    "rut": "EXISTING_TECH_RUT"
  },
  "servicio": {
    "presGPS": "gpsimple",
    "tiposerv": "instalacion_gps",
    "taller_install": "cartec"
  },
  "firma": {
    "imgfirma": "data:image/png;base64,$(cat signature.b64)"
  },
  "vehiculos": [
    {
      "patente": "TEST99",
      "marca": "Toyota",
      "modelo": "Corolla",
      "color": "Blanco",
      "anio": 2020,
      "vin": "TEST123456789",
      "img_vin": "data:image/jpeg;base64,$(cat vin.b64)"
    }
  ]
}
EOF

# 3. Send request
curl -X POST http://localhost:3000/api/ot/create \
  -H "Content-Type: application/json" \
  -d @request_with_images.json
```

## Common Issues

### "Technician not found"

Insert a test technician:

```sql
INSERT INTO Tecnicos (rut_tecnico, nombre, telefono, email) 
VALUES ('98765432-1', 'Test Technician', '+56912345678', 'tech@test.cl');
```

### Port already in use

Change port in `.env`:
```env
PORT=3001
```

### Connection refused

Check MySQL is running:
```bash
systemctl status mysql
```

## Next Steps

- See [README.md](./README.md) for full API documentation
- See [DEPLOYMENT.md](./DEPLOYMENT.md) for production deployment
- See [ENV_SETUP.md](./ENV_SETUP.md) for environment configuration

