TRUNCATE TABLE activities CASCADE;
TRUNCATE TABLE contacts CASCADE;
TRUNCATE TABLE deals CASCADE;
TRUNCATE TABLE organization_members CASCADE;
TRUNCATE TABLE organizations CASCADE;
TRUNCATE TABLE tasks CASCADE;
TRUNCATE TABLE users CASCADE;

-- Пользователи
INSERT INTO users (id, email, hashed_password, name, is_active, created_at)
VALUES
    (1, 'owner@example.com', 'pbkdf2_sha256$260000$demo$Tk5YEtPJj6..', 'Alice Owner', TRUE, now()),
    (2, 'manager@example.com', 'pbkdf2_sha256$260000$demo$Tk5YEtPJj6..', 'Bob Manager', TRUE, now()),
    (3, 'member@example.com',  'pbkdf2_sha256$260000$demo$Tk5YEtPJj6..', 'Carol Member', TRUE, now());

-- Организации
INSERT INTO organizations (id, name, created_at)
VALUES
    (1, 'Acme Corp', now()),
    (2, 'Beta LLC', now());

-- Участники организаций
INSERT INTO organization_members (id, organization_id, user_id, role, created_at)
VALUES
    (1, 1, 1, 'owner', now()),
    (2, 1, 2, 'manager', now()),
    (3, 1, 3, 'member', now()),
    (4, 2, 2, 'owner', now());

-- Контакты (в рамках орг. 1)
INSERT INTO contacts (id, organization_id, owner_id, name, email, phone, created_at)
VALUES
    (1, 1, 2, 'John Doe', 'john.doe@acme.com', '+1-202-555-0101', now()),
    (2, 1, 3, 'Jane Smith', 'jane.smith@acme.com', '+1-202-555-0102', now());

-- Сделки
INSERT INTO deals (
    id, organization_id, contact_id, owner_id, title, amount, currency,
    status, stage, created_at, updated_at
) VALUES
    (1, 1, 1, 2, 'Website Redesign', 15000.00, 'USD', 'in_progress', 'proposal', now(), now()),
    (2, 1, 2, 3, 'Support Contract', 5000.00, 'USD', 'new', 'qualification', now(), now());

-- Задачи
INSERT INTO tasks (
    id, deal_id, title, description, due_date, is_done, created_at
) VALUES
    (1, 1, 'Prepare proposal', 'Draft technical scope', now() + interval '5 days', FALSE, now()),
    (2, 2, 'Call client', 'Discuss onboarding plan', now() + interval '3 days', FALSE, now());

-- Активности
INSERT INTO activities (
    id, deal_id, author_id, type, payload, created_at
) VALUES
    (1, 1, 2, 'comment', '{"text": "Kickoff meeting scheduled"}', now()),
    (2, 1, 2, 'status_changed', '{"old_status": "new", "new_status": "in_progress"}', now()),
    (3, 2, 3, 'task_created', '{"task_id": 2, "title": "Call client"}', now());

SELECT setval('users_id_seq', COALESCE((SELECT MAX(id) FROM users), 0), (SELECT MAX(id) FROM users) IS NOT NULL);
SELECT setval('organizations_id_seq', COALESCE((SELECT MAX(id) FROM organizations), 0), (SELECT MAX(id) FROM organizations) IS NOT NULL);
SELECT setval('organization_members_id_seq', COALESCE((SELECT MAX(id) FROM organization_members), 0), (SELECT MAX(id) FROM organization_members) IS NOT NULL);
SELECT setval('contacts_id_seq', COALESCE((SELECT MAX(id) FROM contacts), 0), (SELECT MAX(id) FROM contacts) IS NOT NULL);
SELECT setval('deals_id_seq', COALESCE((SELECT MAX(id) FROM deals), 0), (SELECT MAX(id) FROM deals) IS NOT NULL);
SELECT setval('tasks_id_seq', COALESCE((SELECT MAX(id) FROM tasks), 0), (SELECT MAX(id) FROM tasks) IS NOT NULL);
SELECT setval('activities_id_seq', COALESCE((SELECT MAX(id) FROM activities), 0), (SELECT MAX(id) FROM activities) IS NOT NULL);