-- Database Surgery Script for Organization and Vehicle Fix
-- Using raw SQL to avoid SQLAlchemy model complexity

-- 1. Fix Private Organization (ID 21) - set owner_id to tester_pro's person_id (29)
UPDATE fleet.organizations 
SET owner_id = 29, updated_at = NOW()
WHERE id = 21 AND org_type = 'individual';

-- 2. Fix Corporate Organization (ID 15) - update owner_id from user_id (28) to person_id (29)
UPDATE fleet.organizations 
SET owner_id = 29, updated_at = NOW()
WHERE id = 15 AND org_type = 'fleet_owner';

-- 3. Create Alpha Organization
INSERT INTO fleet.organizations (
    full_name,
    name,
    folder_slug,
    org_type,
    owner_id,
    status,
    is_active,
    first_registered_at,
    current_lifecycle_started_at,
    lifecycle_index,
    is_anonymized,
    default_currency,
    country_code,
    language,
    subscription_plan,
    base_asset_limit,
    purchased_extra_slots,
    notification_settings,
    external_integration_config,
    is_verified,
    created_at,
    updated_at,
    is_ownership_transferable
) VALUES (
    'Test Kft. Alpha',
    'Test Kft. Alpha',
    'alpha' || substr(md5(random()::text), 1, 6),
    'fleet_owner',
    29,
    'active',
    true,
    NOW(),
    NOW(),
    1,
    false,
    'HUF',
    'HU',
    'hu',
    'FREE',
    1,
    0,
    '{"notify_owner": true, "alert_days_before": [30, 15, 7, 1]}'::jsonb,
    '{}'::jsonb,
    false,
    NOW(),
    NOW(),
    true
) RETURNING id;

-- Save the Alpha org ID for branch creation
WITH alpha_org AS (
    SELECT id FROM fleet.organizations 
    WHERE folder_slug LIKE 'alpha%' AND full_name = 'Test Kft. Alpha'
    ORDER BY created_at DESC LIMIT 1
)
INSERT INTO fleet.branches (organization_id, name, is_main)
SELECT id, 'Alpha Main Garage', true FROM alpha_org;

-- 4. Create Beta Organization
INSERT INTO fleet.organizations (
    full_name,
    name,
    folder_slug,
    org_type,
    owner_id,
    status,
    is_active,
    first_registered_at,
    current_lifecycle_started_at,
    lifecycle_index,
    is_anonymized,
    default_currency,
    country_code,
    language,
    subscription_plan,
    base_asset_limit,
    purchased_extra_slots,
    notification_settings,
    external_integration_config,
    is_verified,
    created_at,
    updated_at,
    is_ownership_transferable
) VALUES (
    'Test Kft. Beta',
    'Test Kft. Beta',
    'beta' || substr(md5(random()::text), 1, 6),
    'fleet_owner',
    29,
    'active',
    true,
    NOW(),
    NOW(),
    1,
    false,
    'HUF',
    'HU',
    'hu',
    'FREE',
    1,
    0,
    '{"notify_owner": true, "alert_days_before": [30, 15, 7, 1]}'::jsonb,
    '{}'::jsonb,
    false,
    NOW(),
    NOW(),
    true
) RETURNING id;

-- Save the Beta org ID for branch creation
WITH beta_org AS (
    SELECT id FROM fleet.organizations 
    WHERE folder_slug LIKE 'beta%' AND full_name = 'Test Kft. Beta'
    ORDER BY created_at DESC LIMIT 1
)
INSERT INTO fleet.branches (organization_id, name, is_main)
SELECT id, 'Beta Main Garage', true FROM beta_org;

-- 5. Get vehicle IDs for distribution
-- First 3 vehicles owned by person_id 29 (tester_pro)
WITH vehicle_ids AS (
    SELECT id, license_plate, ROW_NUMBER() OVER (ORDER BY id) as rn
    FROM vehicle.assets 
    WHERE owner_person_id = 29 
    AND license_plate IS NOT NULL
    ORDER BY id
    LIMIT 3
),
org_ids AS (
    SELECT 
        (SELECT id FROM fleet.organizations WHERE id = 21) as private_org_id,
        (SELECT id FROM fleet.organizations WHERE full_name = 'Test Kft. Alpha') as alpha_org_id,
        (SELECT id FROM fleet.organizations WHERE full_name = 'Test Kft. Beta') as beta_org_id
)
-- Update vehicle 1 to Private Organization
UPDATE vehicle.assets a
SET current_organization_id = o.private_org_id
FROM vehicle_ids v, org_ids o
WHERE a.id = v.id AND v.rn = 1;

-- Update vehicle 2 to Alpha Organization
UPDATE vehicle.assets a
SET current_organization_id = o.alpha_org_id
FROM vehicle_ids v, org_ids o
WHERE a.id = v.id AND v.rn = 2;

-- Update vehicle 3 to Beta Organization
UPDATE vehicle.assets a
SET current_organization_id = o.beta_org_id
FROM vehicle_ids v, org_ids o
WHERE a.id = v.id AND v.rn = 3;

-- 6. Update asset assignments
-- Delete existing assignments for these vehicles
DELETE FROM fleet.asset_assignments 
WHERE asset_id IN (
    SELECT id FROM vehicle.assets 
    WHERE owner_person_id = 29 
    ORDER BY id 
    LIMIT 3
);

-- Create new assignments
WITH vehicle_ids AS (
    SELECT id, ROW_NUMBER() OVER (ORDER BY id) as rn
    FROM vehicle.assets 
    WHERE owner_person_id = 29 
    AND license_plate IS NOT NULL
    ORDER BY id
    LIMIT 3
),
org_ids AS (
    SELECT 
        (SELECT id FROM fleet.organizations WHERE id = 21) as private_org_id,
        (SELECT id FROM fleet.organizations WHERE full_name = 'Test Kft. Alpha') as alpha_org_id,
        (SELECT id FROM fleet.organizations WHERE full_name = 'Test Kft. Beta') as beta_org_id
)
INSERT INTO fleet.asset_assignments (asset_id, organization_id)
SELECT 
    v.id,
    CASE 
        WHEN v.rn = 1 THEN o.private_org_id
        WHEN v.rn = 2 THEN o.alpha_org_id
        WHEN v.rn = 3 THEN o.beta_org_id
    END
FROM vehicle_ids v, org_ids o
WHERE v.rn IN (1, 2, 3);