-- cooking up some better test data for use with findblah

-- my_audio_latency can have a special value of -1, which means 'unknown'.
CREATE OR REPLACE FUNCTION generate_scores_dataset () RETURNS VOID STRICT VOLATILE AS $$
  BEGIN

    delete from GeoIPLocations;
    insert into GeoIPLocations (locId, countryCode, region, city, postalCode, latitude, longitude, metroCode, areaCode) values
      (17192,'US','TX','Austin','78749',30.2076,-97.8587,635,'512'),
      (667,'US','TX','Dallas','75207',32.7825,-96.8207,623,'214'),
      (30350,'US','TX','Houston','77001',29.7633,-95.3633,618,'713'),
      (31423,'US','CO','Denver','80201',39.7392,-104.9847,751,'303'),
      (1807,'US','TX','San Antonio','78201',29.4713,-98.5353,641,'210'),
      (23565,'US','FL','Miami','33101',25.7743,-80.1937,528,'305'),
      (11704,'US','FL','Tampa','33601',27.9475,-82.4584,539,'813'),
      (26424,'US','MA','Boston','02101',42.3584,-71.0598,506,'617'),
      (5059,'US','ME','Portland','04101',43.6589,-70.2615,500,'207'),
      (2739,'US','OR','Portland','97201',45.5073,-122.6932,820,'503'),
      (1539,'US','WA','Seattle','98101',47.6103,-122.3341,819,'206'),
      (2720,'US','CA','Mountain View','94040',37.3845,-122.0881,807,'650'),
      (154078,'US','AR','Mountain View','72560',35.8732,-92.0717,693,'870'),
      (3964,'US','CA','Barstow','92311',34.9701,-116.9929,803,'760'),
      (14447,'US','OK','Tulsa','74101',36.154,-95.9928,671,'918'),
      (162129,'US','TN','Memphis','37501',35.1693,-89.9904,640,'713');

    delete from GeoIPBlocks;
    insert into GeoIPBlocks (beginIp, endIp, locId) values
      (x'00000000'::bigint,x'0FFFFFFF'::bigint,17192),
      (x'10000000'::bigint,x'1FFFFFFF'::bigint,667),
      (x'20000000'::bigint,x'2FFFFFFF'::bigint,30350),
      (x'30000000'::bigint,x'3FFFFFFF'::bigint,31423),
      (x'40000000'::bigint,x'4FFFFFFF'::bigint,1807),
      (x'50000000'::bigint,x'5FFFFFFF'::bigint,23565),
      (x'60000000'::bigint,x'6FFFFFFF'::bigint,11704),
      (x'70000000'::bigint,x'7FFFFFFF'::bigint,26424),
      (x'80000000'::bigint,x'8FFFFFFF'::bigint,5059),
      (x'90000000'::bigint,x'9FFFFFFF'::bigint,2739),
      (x'A0000000'::bigint,x'AFFFFFFF'::bigint,1539),
      (x'B0000000'::bigint,x'BFFFFFFF'::bigint,2720),
      (x'C0000000'::bigint,x'CFFFFFFF'::bigint,154078),
      (x'D0000000'::bigint,x'DFFFFFFF'::bigint,3964),
      (x'E0000000'::bigint,x'EFFFFFFF'::bigint,14447),
      (x'F0000000'::bigint,x'FFFEFFFF'::bigint,162129);
    -- (x'FFFF0000'::bigint,x'FFFFFFFF'::bigint,bogus)

    delete from GeoIPISP;
    insert into GeoIPISP values
      (x'00000000'::bigint,x'0FFFFFFF'::bigint,'Intergalactic Boogie'),
      (x'10000000'::bigint,x'1FFFFFFF'::bigint,'Powerful Pipes'),
      (x'20000000'::bigint,x'2FFFFFFF'::bigint,'Powerful Pipes'),
      (x'30000000'::bigint,x'3FFFFFFF'::bigint,'Intergalactic Boogie'),
      (x'40000000'::bigint,x'4FFFFFFF'::bigint,'Tangled Webs'),
      (x'50000000'::bigint,x'5FFFFFFF'::bigint,'Tangled Webs'),
      (x'60000000'::bigint,x'6FFFFFFF'::bigint,'Powerful Pipes'),
      (x'70000000'::bigint,x'7FFFFFFF'::bigint,'Intergalactic Boogie'),
      (x'80000000'::bigint,x'8FFFFFFF'::bigint,'Greasy Lightning'),
      (x'90000000'::bigint,x'9FFFFFFF'::bigint,'Powerful Pipes'),
      (x'A0000000'::bigint,x'AFFFFFFF'::bigint,'Intergalactic Boogie'),
      (x'B0000000'::bigint,x'BFFFFFFF'::bigint,'Tangled Webs'),
      (x'C0000000'::bigint,x'CFFFFFFF'::bigint,'Greasy Lightning'),
      (x'D0000000'::bigint,x'DFFFFFFF'::bigint,'Tangled Webs'),
      (x'E0000000'::bigint,x'EFFFFFFF'::bigint,'Intergalactic Boogie'),
      (x'F0000000'::bigint,x'FFFEFFFF'::bigint,'Powerful Pipes');
    -- (x'FFFF0000'::bigint,x'FFFFFFFF'::bigint,'bogus')

    DELETE FROM jamcompany;
    ALTER SEQUENCE jamcompany_coid_seq RESTART WITH 1;
    INSERT INTO jamcompany (company) SELECT DISTINCT company FROM geoipisp ORDER BY company;

    DELETE FROM jamisp;
    INSERT INTO jamisp (beginip, endip, coid) SELECT x.beginip, x.endip, y.coid FROM geoipisp x, jamcompany y WHERE x.company = y.company;

    --UPDATE geoiplocations SET geog = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)::geography;
    --UPDATE geoipblocks SET geom = ST_MakeEnvelope(beginip, -1, endip, 1);
    --UPDATE jamisp SET geom = ST_MakeEnvelope(beginip, -1, endip, 1);


    IF EXISTS(
      SELECT *
      FROM information_schema.tables
      WHERE
        table_schema = 'public' AND
        table_name = 'cities') THEN

      DELETE FROM cities;
      INSERT INTO cities (city, region, countrycode) select distinct city, region, countrycode from geoiplocations where length(city) > 0 and length(countrycode) > 0;

      DELETE FROM regions;
      INSERT INTO regions (region, regionname, countrycode) select distinct region, region, countrycode from cities;

      DELETE FROM countries;
      INSERT INTO countries (countrycode, countryname) select distinct countrycode, countrycode from regions;

    END IF;
    RETURN;
  END;
$$ LANGUAGE plpgsql;

SELECT generate_scores_dataset();