-- =====================================================================
-- GUARDiA Mall — 배송지역(68 NJ 시) + 매장 좌표 + 최근접 지점 연결 마이그레이션
-- 대상 DB: mall_db  |  멱등(ON CONFLICT / IF NOT EXISTS)  |  mall 전용
-- =====================================================================
BEGIN;

-- ---------------------------------------------------------------------
-- 1. 매장 좌표 — Montvale Florist 실제 위치 반영
--    41°02'26.8"N 74°01'47.5"W = 41.0408, -74.0299 (store id=1)
-- ---------------------------------------------------------------------
-- lat/lng 컬럼 보장(이미 존재하면 무시)
ALTER TABLE mall_store ADD COLUMN IF NOT EXISTS lat NUMERIC(10,6);
ALTER TABLE mall_store ADD COLUMN IF NOT EXISTS lng NUMERIC(10,6);

UPDATE mall_store SET lat=41.040800, lng=-74.029900 WHERE id=1;

-- 트라이스테이트 확장 매장 합리적 좌표(주변 분산) — 누락/0인 경우만 보정
UPDATE mall_store SET lat=40.885900, lng=-74.043400 WHERE id=2  AND (lat IS NULL);
UPDATE mall_store SET lat=40.944500, lng=-74.071500 WHERE id=3  AND (lat IS NULL);
UPDATE mall_store SET lat=40.850800, lng=-73.970900 WHERE id=4  AND (lat IS NULL);
UPDATE mall_store SET lat=40.892900, lng=-73.972700 WHERE id=5  AND (lat IS NULL);
UPDATE mall_store SET lat=40.979500, lng=-74.116600 WHERE id=6  AND (lat IS NULL);
UPDATE mall_store SET lat=40.940300, lng=-74.131500 WHERE id=7  AND (lat IS NULL);
UPDATE mall_store SET lat=40.897700, lng=-74.016000 WHERE id=8  AND (lat IS NULL);
UPDATE mall_store SET lat=40.881400, lng=-74.108500 WHERE id=9  AND (lat IS NULL);
UPDATE mall_store SET lat=41.090900, lng=-73.917800 WHERE id=10 AND (lat IS NULL);
UPDATE mall_store SET lat=41.088600, lng=-74.013700 WHERE id=11 AND (lat IS NULL);

-- ---------------------------------------------------------------------
-- 2. 배송지역(Delivery Areas) — Montvale Florist 당일배송 68개 시(NJ)
--    도시 단위(같은 당일배송 권역). 대표 ZIP/좌표 포함 → 최근접 거리 계산 근거.
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS mall_delivery_area (
    id BIGSERIAL PRIMARY KEY,
    store_id BIGINT NOT NULL DEFAULT 1,        -- 매핑 매장(Montvale 플래그십=1)
    city VARCHAR(120) NOT NULL,
    state VARCHAR(10) NOT NULL DEFAULT 'NJ',
    zip VARCHAR(10),                            -- 대표 ZIP
    lat NUMERIC(10,6),                          -- 대표 좌표(시청/중심)
    lng NUMERIC(10,6),
    same_day BOOLEAN DEFAULT TRUE,              -- 동일(당일) 배송 표시
    active BOOLEAN DEFAULT TRUE,
    CONSTRAINT uq_mall_delivery_area UNIQUE (city, state)
);
CREATE INDEX IF NOT EXISTS idx_mall_delivery_area_city ON mall_delivery_area(city);
CREATE INDEX IF NOT EXISTS idx_mall_delivery_area_zip  ON mall_delivery_area(zip);

INSERT INTO mall_delivery_area (store_id, city, state, zip, lat, lng, same_day) VALUES
(1,'Allendale','NJ','07401',41.041400,-74.129400,TRUE),
(1,'Alpine','NJ','07620',40.957000,-73.930900,TRUE),
(1,'Bergenfield','NJ','07621',40.927400,-73.997400,TRUE),
(1,'Bogota','NJ','07603',40.875300,-74.029600,TRUE),
(1,'Carlstadt','NJ','07072',40.840300,-74.090300,TRUE),
(1,'Cliffside Park','NJ','07010',40.821300,-73.987800,TRUE),
(1,'Closter','NJ','07624',40.973100,-73.961800,TRUE),
(1,'Cresskill','NJ','07626',40.941900,-73.959700,TRUE),
(1,'Demarest','NJ','07627',40.957300,-73.962800,TRUE),
(1,'Dumont','NJ','07628',40.940500,-73.992700,TRUE),
(1,'East Rutherford','NJ','07073',40.836700,-74.097900,TRUE),
(1,'Edgewater','NJ','07020',40.827500,-73.974700,TRUE),
(1,'Elmwood Park','NJ','07407',40.904100,-74.118500,TRUE),
(1,'Emerson','NJ','07630',40.976300,-74.026900,TRUE),
(1,'Englewood Cliffs','NJ','07632',40.886000,-73.952200,TRUE),
(1,'Englewood','NJ','07631',40.892900,-73.972700,TRUE),
(1,'Fair Lawn','NJ','07410',40.940300,-74.131500,TRUE),
(1,'Fairview','NJ','07022',40.811900,-74.001100,TRUE),
(1,'Fort Lee','NJ','07024',40.850800,-73.970900,TRUE),
(1,'Franklin Lakes','NJ','07417',41.013800,-74.207300,TRUE),
(1,'Garfield','NJ','07026',40.881400,-74.108500,TRUE),
(1,'Glen Rock','NJ','07452',40.962900,-74.132900,TRUE),
(1,'Hackensack','NJ','07601',40.885900,-74.043400,TRUE),
(1,'Harrington Park','NJ','07640',40.985100,-73.980200,TRUE),
(1,'Hasbrouck Heights','NJ','07604',40.858500,-74.075000,TRUE),
(1,'Haworth','NJ','07641',40.960600,-73.991200,TRUE),
(1,'Hillsdale','NJ','07642',41.000200,-74.040000,TRUE),
(1,'Ho-Ho-Kus','NJ','07423',40.998900,-74.103200,TRUE),
(1,'Leonia','NJ','07605',40.862400,-73.989900,TRUE),
(1,'Little Ferry','NJ','07643',40.851800,-74.040400,TRUE),
(1,'Lodi','NJ','07644',40.882600,-74.083100,TRUE),
(1,'Lyndhurst','NJ','07071',40.812200,-74.124200,TRUE),
(1,'Mahwah','NJ','07430',41.087400,-74.159000,TRUE),
(1,'Maywood','NJ','07607',40.902000,-74.061800,TRUE),
(1,'Midland Park','NJ','07432',40.989400,-74.142200,TRUE),
(1,'Millburn','NJ','07041',40.725800,-74.302900,TRUE),
(1,'Montvale','NJ','07645',41.040800,-74.029900,TRUE),
(1,'Moonachie','NJ','07074',40.842500,-74.046500,TRUE),
(1,'New Milford','NJ','07646',40.934700,-74.019100,TRUE),
(1,'North Arlington','NJ','07031',40.788400,-74.133400,TRUE),
(1,'Northvale','NJ','07647',41.007400,-73.948300,TRUE),
(1,'Norwood','NJ','07648',40.994400,-73.958800,TRUE),
(1,'Oakland','NJ','07436',41.029900,-74.265400,TRUE),
(1,'Oradell','NJ','07649',40.955700,-74.034000,TRUE),
(1,'Palisades Park','NJ','07650',40.848200,-73.997300,TRUE),
(1,'Paramus','NJ','07652',40.944500,-74.071500,TRUE),
(1,'Park Ridge','NJ','07656',41.038200,-74.041700,TRUE),
(1,'Ramsey','NJ','07446',41.057300,-74.141100,TRUE),
(1,'Ridgefield Park','NJ','07660',40.857200,-74.022600,TRUE),
(1,'Ridgefield','NJ','07657',40.832800,-74.001500,TRUE),
(1,'Ridgewood','NJ','07450',40.979500,-74.116600,TRUE),
(1,'River Edge','NJ','07661',40.927300,-74.039600,TRUE),
(1,'Rochelle Park','NJ','07662',40.906900,-74.077800,TRUE),
(1,'Rutherford','NJ','07070',40.826600,-74.106500,TRUE),
(1,'Saddle Brook','NJ','07663',40.902300,-74.092700,TRUE),
(1,'Saddle River','NJ','07458',41.029900,-74.099500,TRUE),
(1,'Secaucus','NJ','07094',40.789000,-74.056500,TRUE),
(1,'South Hackensack','NJ','07606',40.861700,-74.050900,TRUE),
(1,'Teaneck','NJ','07666',40.897700,-74.016000,TRUE),
(1,'Tenafly','NJ','07670',40.925700,-73.962900,TRUE),
(1,'Teterboro','NJ','07608',40.857800,-74.060800,TRUE),
(1,'Township Of Washington','NJ','07676',40.992000,-74.063000,TRUE),
(1,'Waldwick','NJ','07463',41.012000,-74.118600,TRUE),
(1,'Wallington','NJ','07057',40.853100,-74.106000,TRUE),
(1,'Westwood','NJ','07675',40.991900,-74.032900,TRUE),
(1,'Wood Ridge','NJ','07075',40.844700,-74.087500,TRUE),
(1,'Woodcliff Lake','NJ','07677',41.022300,-74.062700,TRUE),
(1,'Wyckoff','NJ','07481',41.009600,-74.172600,TRUE)
ON CONFLICT (city, state) DO UPDATE
   SET store_id=EXCLUDED.store_id, zip=EXCLUDED.zip, lat=EXCLUDED.lat, lng=EXCLUDED.lng,
       same_day=EXCLUDED.same_day, active=TRUE;

COMMIT;

-- 검증
SELECT id, name, lat, lng FROM mall_store WHERE id=1;
SELECT COUNT(*) AS delivery_area_count FROM mall_delivery_area WHERE active=TRUE;
