﻿import psycopg2
from datetime import datetime, date, timedelta
from werkzeug.security import generate_password_hash

conn = psycopg2.connect('postgresql://postgres:wd2SZgQ4qfFZo4Z@localhost:5432/rateright_local')
cur = conn.cursor()

now = datetime.now()
password_hash = generate_password_hash('password', method='pbkdf2:sha256')

# 1. Create minimal categories
cur.execute("""INSERT INTO categories (id, name, whs_risk_level, license_required, created_at, updated_at) VALUES
    (1, 'General', 'low', false, %s, %s),
    (2, 'Plumbing', 'medium', false, %s, %s)
    ON CONFLICT (id) DO NOTHING""", (now, now, now, now))

# 2. Create test users
cur.execute("""INSERT INTO users (username, email, first_name, last_name, password_hash, role, created_at, updated_at) VALUES
    ('contractor1', 'contractor@test.com', 'Test', 'Contractor', %s, 'contractor', %s, %s),
    ('worker1', 'worker@test.com', 'Test', 'Worker', %s, 'worker', %s, %s)
    ON CONFLICT (email) DO NOTHING RETURNING id""",
    (password_hash, now, now, password_hash, now, now))

# 3. Get user IDs
cur.execute("SELECT id FROM users WHERE email IN ('contractor@test.com', 'worker@test.com') ORDER BY email")
users = cur.fetchall()
if len(users) == 2:
    contractor_id, worker_id = [r[0] for r in users]
else:
    cur.execute("SELECT id FROM users LIMIT 2")
    contractor_id, worker_id = [r[0] for r in cur.fetchall()]

# 4. Create a job
cur.execute("""INSERT INTO jobs (contractor_id, category_id, title, description, location,
    hourly_rate, status, created_at, updated_at) VALUES
    (%s, 1, 'Test Job', 'Test job for signatures', 'Sydney', 50, 'open', %s, %s)
    RETURNING id""", (contractor_id, now, now))
job_id = cur.fetchone()[0]

# 5. Create a contract ready to sign
cur.execute("""INSERT INTO contracts (job_id, contractor_id, worker_id, agreed_rate, rate_type,
    start_date, end_date, scope_of_work, status, contractor_reviewed, worker_reviewed,
    created_at, updated_at) VALUES
    (%s, %s, %s, 50, 'hourly', %s, %s, 'Test work', 'pending_agreement', true, true, %s, %s)
    RETURNING id""",
    (job_id, contractor_id, worker_id, date.today(), date.today() + timedelta(7), now, now))
contract_id = cur.fetchone()[0]

conn.commit()
print(f"Created contract {contract_id}")
print("Login: contractor@test.com / password")
print("URL: http://localhost:5000/contracts")
