CREATE DATABASE IF NOT EXISTS milk_collection_db;
USE milk_collection_db;

CREATE TABLE IF NOT EXISTS customers (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    address VARCHAR(255),
    phone VARCHAR(20),
    assigned_collector_id INT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (assigned_collector_id) REFERENCES users(id) ON DELETE SET NULL
);

CREATE TABLE IF NOT EXISTS collections (
    id INT AUTO_INCREMENT PRIMARY KEY,
    collector_id INT NOT NULL,
    customer_id INT NOT NULL,
    amount_ml INT NOT NULL,
    collection_date DATE NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (collector_id) REFERENCES users(id),
    FOREIGN KEY (customer_id) REFERENCES customers(id)
);

-- Insert sample customers (ilk ikisi toplayici1'e atanmış)
INSERT INTO customers (name, address, phone, assigned_collector_id) VALUES 
('Mehmet Demir Çiftliği', 'Bahçelievler Mah. No:45', '0532 123 4567', 2),
('Ayşe Kaya Mandırası', 'Merkez Mah. No:78', '0543 234 5678', 2),
('Fatma Yıldız Çiftliği', 'Yeni Mah. No:23', '0555 345 6789', NULL);
