"""add motornet_valuations table

Revision ID: 0002
Revises: 0001
Create Date: 2026-04-28 18:00:00.000000

"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa

revision: str = "0002"
down_revision: Union[str, None] = "0001"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.execute("""
        CREATE TABLE IF NOT EXISTS motornet_valuations (
            id SERIAL PRIMARY KEY,
            valuation_id INTEGER NOT NULL REFERENCES valuations(id),
            plate VARCHAR(10) NOT NULL,
            motornet_code VARCHAR(20),
            fetched_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

            registration_year INTEGER,
            registration_month INTEGER,
            mileage INTEGER,

            motornet_id INTEGER,
            ediz_dati VARCHAR(20),
            brand_name VARCHAR(100),
            model_description VARCHAR(200),
            allestimento VARCHAR(255),
            alimentazione VARCHAR(100),
            immagine_url VARCHAR(500),
            xml_url VARCHAR(500),
            percorrenza_media_km INTEGER,

            quotazione_blu NUMERIC(12,2),
            quotazione_blu_km NUMERIC(12,2),
            quotazione_giallo NUMERIC(12,2),
            quotazione_giallo_km NUMERIC(12,2),
            quotazione_blu_totale NUMERIC(12,2),
            quotazione_giallo_totale NUMERIC(12,2),
            variazione_km NUMERIC(12,2),

            prezzo_listino NUMERIC(12,2),
            prezzo_accessori NUMERIC(12,2),
            totale_riparazioni_carrozzeria NUMERIC(12,2),
            totale_riparazioni_meccanica NUMERIC(12,2),

            raw_response JSONB
        );

        CREATE INDEX IF NOT EXISTS ix_motornet_valuations_valuation_id ON motornet_valuations(valuation_id);
        CREATE INDEX IF NOT EXISTS ix_motornet_valuations_plate ON motornet_valuations(plate);
    """)


def downgrade() -> None:
    op.execute("DROP TABLE IF EXISTS motornet_valuations;")
