-- CannaManage System Test Seed Data
-- This file is mounted into PostgreSQL as /docker-entrypoint-initdb.d/99-seed.sql
-- It runs AFTER Flyway migrations (which use Spring Boot on backend startup).
-- NOTE: Since Flyway runs on backend start (not on DB init), this file will only
-- work if the tables already exist. For Docker test profile, the backend's Flyway
-- creates tables first, then this seed is loaded via the seed container's curl calls.
-- This SQL is kept as a reference and can be loaded manually:
--   docker exec -i cannamanage-db psql -U cannamanage -d cannamanage < scripts/seed/init.sql

-- ============================================================================
-- Test Club
-- ============================================================================
INSERT INTO clubs (id, tenant_id, name, address, license_number, max_members, status, max_prevention_officers)
VALUES (
    'a1000000-0000-0000-0000-000000000001',
    'a1000000-0000-0000-0000-000000000001',
    'Grüner Daumen e.V.',
    'Hanfstraße 42, 10115 Berlin',
    'CSC-BER-2024-001',
    500,
    'ACTIVE',
    2
) ON CONFLICT (license_number) DO NOTHING;

-- ============================================================================
-- Test Admin User (email: admin@test.de, password: test123)
-- BCrypt hash of "test123" generated via: python3 -c "import bcrypt; print(bcrypt.hashpw(b'test123', bcrypt.gensalt(10)).decode())"
-- ============================================================================
INSERT INTO users (id, tenant_id, email, password_hash, role, active)
VALUES (
    'b1000000-0000-0000-0000-000000000001',
    'a1000000-0000-0000-0000-000000000001',
    'admin@test.de',
    '$2a$10$/FgU1KyveJ7MaQ7Xv4kxD.5EIQUHujJfZI4K2E1H7pS6parMHJpeG',
    'ROLE_ADMIN',
    true
) ON CONFLICT (email, tenant_id) DO NOTHING;

-- ============================================================================
-- Test Members (5 members)
-- ============================================================================
INSERT INTO members (id, tenant_id, club_id, first_name, last_name, email, date_of_birth, membership_date, membership_number, status, is_under_21)
VALUES
    ('c1000000-0000-0000-0000-000000000001', 'a1000000-0000-0000-0000-000000000001', 'a1000000-0000-0000-0000-000000000001',
     'Max', 'Mustermann', 'max@test.de', '1990-05-15', '2024-01-15', 'M-001', 'ACTIVE', false),
    ('c1000000-0000-0000-0000-000000000002', 'a1000000-0000-0000-0000-000000000001', 'a1000000-0000-0000-0000-000000000001',
     'Anna', 'Schmidt', 'anna@test.de', '1985-08-22', '2024-01-20', 'M-002', 'ACTIVE', false),
    ('c1000000-0000-0000-0000-000000000003', 'a1000000-0000-0000-0000-000000000001', 'a1000000-0000-0000-0000-000000000001',
     'Jonas', 'Weber', 'jonas@test.de', '2005-03-10', '2024-02-01', 'M-003', 'ACTIVE', true),
    ('c1000000-0000-0000-0000-000000000004', 'a1000000-0000-0000-0000-000000000001', 'a1000000-0000-0000-0000-000000000001',
     'Lisa', 'Meyer', 'lisa@test.de', '1992-11-30', '2024-02-15', 'M-004', 'ACTIVE', false),
    ('c1000000-0000-0000-0000-000000000005', 'a1000000-0000-0000-0000-000000000001', 'a1000000-0000-0000-0000-000000000001',
     'Tom', 'Fischer', 'tom@test.de', '1988-07-04', '2024-03-01', 'M-005', 'ACTIVE', false)
ON CONFLICT (email, tenant_id) DO NOTHING;

-- ============================================================================
-- Test Strains
-- ============================================================================
INSERT INTO strains (id, tenant_id, name, thc_percentage, cbd_percentage, description)
VALUES
    ('d1000000-0000-0000-0000-000000000001', 'a1000000-0000-0000-0000-000000000001',
     'Northern Lights', 18.50, 0.80, 'Classic indica strain with relaxing effects'),
    ('d1000000-0000-0000-0000-000000000002', 'a1000000-0000-0000-0000-000000000001',
     'Amnesia Haze', 22.00, 1.20, 'Potent sativa with cerebral high'),
    ('d1000000-0000-0000-0000-000000000003', 'a1000000-0000-0000-0000-000000000001',
     'CBD Critical Mass', 5.00, 12.00, 'High-CBD medical strain')
ON CONFLICT DO NOTHING;

-- ============================================================================
-- Test Batches (3 batches)
-- ============================================================================
INSERT INTO batches (id, tenant_id, strain_id, quantity_grams, harvest_date, batch_code, status)
VALUES
    ('e1000000-0000-0000-0000-000000000001', 'a1000000-0000-0000-0000-000000000001',
     'd1000000-0000-0000-0000-000000000001', 500.00, '2024-11-01', 'BATCH-2024-001', 'AVAILABLE'),
    ('e1000000-0000-0000-0000-000000000002', 'a1000000-0000-0000-0000-000000000001',
     'd1000000-0000-0000-0000-000000000002', 300.00, '2024-11-15', 'BATCH-2024-002', 'AVAILABLE'),
    ('e1000000-0000-0000-0000-000000000003', 'a1000000-0000-0000-0000-000000000001',
     'd1000000-0000-0000-0000-000000000003', 200.00, '2024-12-01', 'BATCH-2024-003', 'AVAILABLE')
ON CONFLICT (batch_code, tenant_id) DO NOTHING;

-- ============================================================================
-- Test Distributions (some sample handouts)
-- ============================================================================
INSERT INTO distributions (id, tenant_id, member_id, batch_id, quantity_grams, distributed_at, recorded_by, notes)
VALUES
    ('f1000000-0000-0000-0000-000000000001', 'a1000000-0000-0000-0000-000000000001',
     'c1000000-0000-0000-0000-000000000001', 'e1000000-0000-0000-0000-000000000001',
     5.00, '2024-12-10 10:00:00+01', 'c1000000-0000-0000-0000-000000000001', 'Regular distribution'),
    ('f1000000-0000-0000-0000-000000000002', 'a1000000-0000-0000-0000-000000000001',
     'c1000000-0000-0000-0000-000000000002', 'e1000000-0000-0000-0000-000000000001',
     3.00, '2024-12-10 11:00:00+01', 'c1000000-0000-0000-0000-000000000001', NULL),
    ('f1000000-0000-0000-0000-000000000003', 'a1000000-0000-0000-0000-000000000001',
     'c1000000-0000-0000-0000-000000000003', 'e1000000-0000-0000-0000-000000000003',
     2.00, '2024-12-11 09:30:00+01', 'c1000000-0000-0000-0000-000000000001', 'Under-21 member, CBD only')
ON CONFLICT DO NOTHING;

-- ============================================================================
-- Monthly Quotas for test members (current month)
-- ============================================================================
INSERT INTO monthly_quotas (id, tenant_id, member_id, year, month, total_distributed, max_allowed)
VALUES
    ('g1000000-0000-0000-0000-000000000001', 'a1000000-0000-0000-0000-000000000001',
     'c1000000-0000-0000-0000-000000000001', 2024, 12, 5.00, 50.00),
    ('g1000000-0000-0000-0000-000000000002', 'a1000000-0000-0000-0000-000000000001',
     'c1000000-0000-0000-0000-000000000002', 2024, 12, 3.00, 50.00),
    ('g1000000-0000-0000-0000-000000000003', 'a1000000-0000-0000-0000-000000000001',
     'c1000000-0000-0000-0000-000000000003', 2024, 12, 2.00, 30.00)
ON CONFLICT (member_id, year, month) DO NOTHING;

-- ============================================================================
-- Stock Movements (audit trail)
-- ============================================================================
INSERT INTO stock_movements (id, tenant_id, batch_id, movement_type, quantity_grams, reason)
VALUES
    ('h1000000-0000-0000-0000-000000000001', 'a1000000-0000-0000-0000-000000000001',
     'e1000000-0000-0000-0000-000000000001', 'HARVEST_IN', 500.00, 'Initial harvest intake'),
    ('h1000000-0000-0000-0000-000000000002', 'a1000000-0000-0000-0000-000000000001',
     'e1000000-0000-0000-0000-000000000002', 'HARVEST_IN', 300.00, 'Initial harvest intake'),
    ('h1000000-0000-0000-0000-000000000003', 'a1000000-0000-0000-0000-000000000001',
     'e1000000-0000-0000-0000-000000000003', 'HARVEST_IN', 200.00, 'Initial harvest intake'),
    ('h1000000-0000-0000-0000-000000000004', 'a1000000-0000-0000-0000-000000000001',
     'e1000000-0000-0000-0000-000000000001', 'DISTRIBUTION', -8.00, 'Distributed to members')
ON CONFLICT DO NOTHING;
